Extract array data into a file with yq4

Viewed 148

I have a yaml file say sample.yaml with the following structure

items:
  - version: v1
    data:
      file1.json: |-
        {
            "key1": val1,
            "key2": val2
        }
  - version: v2
    data:
      file2.json: |-
        {
            "key3": val3,
            "key4": val4
        }

In order to achieve the following output by splitting the data into separate files, what commands/operators should I be using in yq4? The documentation is rather confusing.

f1.json:

{
    "key1": val1,
    "key2": val2
}

f2.json:

{
    "key3": val3,
    "key4": val4
}

With yq3, I was able to run

files=$(yq read sample.yaml "items[*].data.*" -pp)
for file in $files; do
    yq read sample.yaml "$file" > f1.json
done

Any pointers would be greatly appreciated. Thanks!

1 Answers

Using mikefarah/yq v4.14.1+ with from_json and the -oj option to convert to JSON, and the -s option to split up into files

yq -oj -s '"f" + ($index + 1)' '.items[].data[] | from_json' sample.yaml

will produce f1.json:

{
  "key1": "val1",
  "key2": "val2"
}

and f2.json:

{
  "key3": "val3",
  "key4": "val4"
}

If you want to use the filenames from the keys instead, you need to employ to_entries to have access to .key and .value. This will produce the files file1.json and file2.json:

yq -oj -s 'parent | .key | sub("\.json$","")' \
  '(.items[].data | to_entries)[].value | from_json' sample.yaml
Related