So8res
So8res

Reputation: 10356

Yaml hierarchical data

I'd like to represent a hierarchy in yaml, and I'm not sure how. For example, I'd like to say something like this:

name: "user1"
programming-skill: 3
    java: 2
    python: 2
cooking-skill: 4

When I throw this at a yaml parser, I get an error along the lines of "mapping values not allowed here" on the line java: 2 because I'm trying to assign programming-skill to both 3 and the list {java: 2, python: 2}.

What's the cleanest way to represent this hierarchical structure in yaml? Alternatively, is there a serialization format more suited than yaml to hierarchical structures?

Upvotes: 6

Views: 7869

Answers (3)

Philip Durbin
Philip Durbin

Reputation: 4082

name: "user1"
programming-skill: 
  - 3
  - java: 2
    python: 2
cooking-skill: 4

or if you prefer:

name: "user1"
programming-skill: 
  - 3
  - 
    java: 2
    python: 2
cooking-skill: 4

Upvotes: 8

So8res
So8res

Reputation: 10356

It looks like it's not possible to cleanly do this in yaml. You have to, as Andrey suggested, introduce new data (others/value) and work around it. I'm still not sure if there's a format that's better suited for this form of data, or if you just have to bite the bullet and introduce an extra level of hierarchy.

Upvotes: 0

Andrey
Andrey

Reputation: 3001

name: "user1"
programming-skill:
        others:
            java: 2
            python: 2
        value: 3
cooking-skill: 4

Test it here

Upvotes: 6

Related Questions