Convert YaML flow style to YaML block style

Viewed 1015

I have some large YAML files that were originally JSON, so the they are in YAML flow style. E.g.

{
    "foo":
    {
        # Some comment
        "bar": "baz", # Another comment
        "qux" : [
            1,
            2,
            3
        ]
    },
    # ...
}

I would like to convert them to YaML block style, preserving comments and (if possible) the object key ordering. E.g.

foo:
  # Some comment
  bar: baz # Another comment
  qux:
    - 1
    - 2
    - 3
# ...

Is this doable?

1 Answers

It's absolutely doable; I can think of two ways to do it offhand. If you're a proficient coder, you can use your language of choice to read in the existing YAML files and then write out the files in the correct format.

Alternately, you can just use your preferred text editor to remove the superfluous characters etc. and clean up the file. Could be arduous though.

The last option, one I don't recommend but feel like I'm obliged to mention, is using regex to convert the file. It's probably possible, but it'd be enormously complicated. I'd advise option A.

Related