Building an array of dictionary items in YAML?

You probably have been looking at YAML for too long because that what you call JSON in your post isn't, it is more a half-and-half of YAML and JSON. Lets skip the fact that JSON doesn't allow comments starting with a #, you should quote the strings that are keys and you should put , between elements in mapping:

{
"models":
 [
  {
    "model": "a",
    "type": "x"
  },
  {
    "model": "b",
    "type": "y"
  }
 ]
}

That is correct JSON as well as it is YAML, because YAML is a superset of JSON. You can e.g. check that online at this YAML parser.

You can convert it to the block-style you seem to prefer as YAML using ruamel.yaml.cmd (based on my enhanced version of PyYAML: pip install ruamel.yaml.cmd). You can use its commandline utility to convert JSON to block YAML (in version 0.9.1 you can also force flow style):

yaml json in.json

which gets you:

models:
- model: a
  type: x
- model: b
  type: y

There are some online resources that allow you to do the above, but as with any of such services, don't use them for anything important (like the list of credit-card numbers and passwords).


Use a dash to start a new list element:

models:
 - model: "a"
   type: "x"
   #bunch of properties...
 - model: "b"
   type: "y"
   #bunch of properties...

Tags:

Yaml

Pyyaml