About YAML

Based on: https://www.json2yaml.com/convert-yaml-to-json

JSON

JSON stands for “javascript object notation”

  • records separated by commas
  • keys & strings wrapped by double quotes
  • good choice for data transport

YAML

YAML stands for “YAML ain’t markup language” and is a superset of JSON

  • lists begin with a hyphen
  • dependent on whitespace / indentation
  • better suited for configuration than json

 

 

YAML vs JSON

YAML is best suited for configuration where JSON is better as a serialization format or serving up data for your APIs.

YAML has a different feature set than JSON, including but not limited to:

  • the ability to self reference
  • support for complex datatypes
  • embedded block literals
  • comments

It is also better suited for human readability

YAML parsers are younger and have also been known to be less secure

JSON vs YAML

JSON is best suited as a serialization format. It is much more explicit and strict than YAML. It is a very popular format for transmitting data over http.

YAML is a superset of JSON, which means you can parse JSON with a YAML parser.

 

Do I need quotes for strings in YAML?

  • In general, you don’t need quotes.
  • Use quotes to force a string and if your value includes special characters
    • Single quote won’t try to parse escape codes. \n is backslash and letter n.
    • Double quote which is similar to JSON double quote. \n is new line character.

How do I break a string over multiple lines?

# Single newline at end (clip)<kdslfj
MultipleLines: |
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  with multiple carriage returns appended to the end of each line.
  
  
# Single newline at end (clip)
LongLine: >
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  with only a single carriage return appended to the end.
  
  
# No newline at end (strip)
Description: >-
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  with NO carriage returns.

See: https://yaml-multiline.info/

Object vs Array containing an object

can be confusion because they look similar in some cases.

# Object
Value: 
  GetAtt:
    -MyRDSInstance
    -Endpoint.Address

Value: {
  "GetAtt": [
    "MyRDSInstance",
    "Endpoint.Address"
  ]
}
  

# Array containing an object
Values:
  -GetAtt:
    -MyRDSInstance
    -Endpoint.Address

Values: [
  {
     "GetAtt": [
      "MyRDSInstance",
      "Endpoint.Address"
    ]
  }
]

 

Also see

Leave a Comment

Your email address will not be published. Required fields are marked *