2 ms·
One interesting demonstration of YAML's complexity relative to JSON is that YAML is almost a complete superset of JSON. This is acknowledged by the authors of t
by jstrieb 5y ago
One interesting demonstration of YAML's complexity relative to JSON is that YAML is almost a complete superset of JSON. This is acknowledged by the authors of the YAML spec.[0]
For example, the following code translates JSON to YAML using only the Python yaml library:
import yaml
data = yaml.load("""
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
""")
print(yaml.dump(data))
Prints:
address:
city: New York
postalCode: 10021-3100
state: NY
streetAddress: 21 2nd Street
age: 27
children: []
firstName: John
isAlive: true
lastName: Smith
phoneNumbers:
- number: 212 555-1234
type: home
- number: 646 555-4567
type: office
- number: 123 456-7890
type: mobile
spouse: null
0: https://yaml.org/spec/1.2.2/#12-yaml-history https://yaml.org/spec/1.2.2/#12-yaml-history
- jborean93 5y agoFurther to this you can even include the trailing commas and hack in comments in the yaml-like json value: import yaml data = yaml.load(""" { "firstName": "John", # Woah comments "lastName": "Smith", } """, yaml.SafeLoader) Granted I still like YAML when it sticks to the basics and avoid some of the messier aspects, like anchors, when I can.