Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple line key in YAML

Tags:

yaml

Is it possible to have a multiple line key like this?

mykey:
  - >
    key
    one:
    keytwo: val

where keyone is treated as one key. I want to parse the yaml to yield:

{ mykey: [ { keyone: { keytwo: val } } ] }
like image 646
kimh Avatar asked Jul 17 '26 14:07

kimh


1 Answers

You can have a multi-line key in YAML, but not quite in the way you describe. In a YAML mapping you can split the key and value onto separate lines by prefixing the key with ? and the value with :, like so:

? foo
: bar

The above would yield a data structure like { "foo": "bar" } in JSON. The YAML spec calls this an explicit key (whereas the usual foo: bar style is implicit). When you use the explicit style, the key can be any YAML data structure, including multi-line scalars:

mykey:
  - ? key
      one
    : keytwo: val

...but, like all multi-line scalars in YAML, even though lines are merged, one space will be preserved between the content of each line, so the above will result in a data structure like the following JSON:

{ "mykey":
  [ { "key one":
      { "keytwo": "val" }
    }
  ]
}

So you end up with key one instead of keyone, which isn't exactly what you wanted. But it's the closest you're going to get with YAML.

like image 56
Jordan Running Avatar answered Jul 23 '26 11:07

Jordan Running