Friday, 14 February 2020

YAML Syntax and Conversion to JSON

YAML is a good markup lang for config, it's simpler than JSON, and it has comments, although JSON5 allows comments too.

Tool:
https://codebeautify.org/yaml-to-json-xml-csv

YAML syntax:
1) Colon, newline, indent, key --> Object
2) Colon, newline, indent, dash, value --> List of values
3) Colon, newline, indent, dash, key, colon, value --> List of objects

Example YAML:
#Object
objkey:
  subkey1: "foo"
  subkey2: "bar"
  
#List of values
listkey1:
  - "foo"
  - "bar"
  
#List of objects
listkey2:
  - foo1: "foo1"
    foo2: "foo2"
  - bar1: "bar1"
    bar2: "bar2"

#Shorthands    
#List of values (single line)
listkey3: ["foo","bar"]

Conversion to JSON:
{
  "objkey": {
    "subkey1": "foo",
    "subkey2": "bar"
  },

  "listkey1": [
    "foo",
    "bar"
  ],

  "listkey2": [
    {
      "foo1": "foo1",
      "foo2": "foo2"
    },
    {
      "bar1": "bar1",
      "bar2": "bar2"
    }
  ],
  "listkey3":["foo","bar"]
}

No comments:

Post a Comment