1

I would like to represent tree structures in YAML. Each node of the tree is composed of a (key,value) pair. The key must be a string.

A structure like this would be perfect:

    node1:
      child_A:
        child_AA:
        child_AB: 1.2
      child_B:
    node2:

...except that I sometimes have nodes that have values but are not leaves. For example, let's add a child to child_AB:

    node1:
      child_A:
        child_AA:
        child_AB: 1.2
          child_ABA: 3.14
      child_B:
    node2:

This is not valid YAML anymore!

Is there a nice way to correct the last YAML document to make it work while staying true to this (key,value) tree structure?

This one is correct :

    node1:
      child_A:
        child_AA:
        child_AB:
          : 1.2 # using empty string, could also use 'value' or similar
          child_ABA: 3.14
      child_B:
    node2:

But I feel it's a shame that something like the previous one is not valid. Any suggestion? YAML extensions? Alternatives languages?

Bérenger
  • 131
  • 3

1 Answers1

1

This is where it becomes useful to separate the value from the child nodes. While this doesn't give you a perfect representation of your desired structure, it is valid YAML. Sometimes you just need to work within the constraints of a given technology, or choose a technology that better models your data structure.

node1:
  child1:
    value: 1.2
    children:
      - value: 3.14
        children:
          - value: "..."
            children:
            - and on it goes
Greg Burghardt
  • 34,276
  • 8
  • 63
  • 114