JSON TO YAML

2022/4/9 Javascript 大约 1 分钟

需求是在web页面显示一个代码块区域,而语法格式是yaml,但是接口响应数据是json。

# 方法一

  1. 安装json2yaml依赖包;传送门 (opens new window)

    npm install -D json2yaml
    
    1
  2. 使用:input.json内容如下

    {
        "json": [
            "rigid",
            "better for data interchange"
        ],
        "yaml": [
            "slim and flexible",
            "better for configuration"
        ],
        "object": {
            "key": "value",
            "array": [
                {
                    "null_value": null
                },
                {
                    "boolean": true
                },
                {
                    "integer": 1
                },
                {
                    "alias": "aliases are like variables"
                },
                {
                    "alias": "aliases are like variables"
                }
            ]
        },
        "paragraph": "Blank lines denote\nparagraph breaks\n",
        "content": "Or we\ncan auto\nconvert line breaks\nto save space",
        "alias": {
            "bar": "baz"
        },
        "alias_reuse": {
            "bar": "baz"
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    import YAML from 'json2yaml';
    const json = require('./input.json');
    
    const yaml = YAML.stringify(json);
    console.log(yaml);
    
    1
    2
    3
    4
    5
    ---
      json: 
        - "rigid"
        - "better for data interchange"
      yaml: 
        - "slim and flexible"
        - "better for configuration"
      object: 
        key: "value"
        array: 
          - 
            null_value: null
          - 
            boolean: true
          - 
            integer: 1
          - 
            alias: "aliases are like variables"
          - 
            alias: "aliases are like variables"
      paragraph: "Blank lines denote\nparagraph breaks\n"
      content: "Or we\ncan auto\nconvert line breaks\nto save space"
      alias: 
        bar: "baz"
      alias_reuse: 
        bar: "baz"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26

    上面的yaml文件就是json转化来的,美中不足就是对于数组array出现了换行,而\n换行符也没有识别出来,不过可以使用;

# 方法二

  1. 安装json-to-pretty-yaml依赖包;传送门 (opens new window)

    npm install --save json-to-pretty-yaml
    
    1
  2. 使用:

    import YAML from 'json-to-pretty-yaml';
    const json = require('./input.json');
    
    const yaml = YAML.stringify(json);
    console.log(yaml);
    
    1
    2
    3
    4
    5
    json:
      - "rigid"
      - "better for data interchange"
    yaml:
      - "slim and flexible"
      - "better for configuration"
    object:
      key: "value"
      array:
        - null_value: null
        - boolean: true
        - integer: 1
        - alias: "aliases are like variables"
        - alias: "aliases are like variables"
    paragraph: "Blank lines denote\nparagraph breaks\n"
    content: "Or we\ncan auto\nconvert line breaks\nto save space"
    alias:
      bar: "baz"
    alias_reuse:
      bar: "baz"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    上面的yaml文件就是json转化来的,相比第一种方法好了很多,但是\n换行符也没有识别出来,不过可以正常使用;

上次编辑于: 2023年7月4日 09:36