1

I say this stack overflow anser with a simple jq command to convert a JSON to a csv file, but I need to improve it further.

Say I have the following JSON:

[
    {
        "name": "foo",
        "env": "dev",
        "version": "1.24"
    },
    {
        "name": "bar",
        "env": "staging",
        "version": "1.21"
    },
    {
        "name": "boo",
        "env": "prod",
        "version": "1.23"
    },
    {
        "name": "far",
        "env": "prod",
        "version": "1.24"
    }
]

How does one create the CSV with only the "name" and "version" fields?

My current command is:

jq -r '(map(keys) | add | unique) as $cols | map(.[] | {name, version} as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'

This is not working. Can anyone provide some help?

Thanks!

1 Answers1

3

If you know the two column names anyway, you could simply extract them directly using .name and .version:

<file jq -r '["name", "version"], (.[] | [.name, .version]) | @csv'

You can also use your $cols variable, so the names only appear once:

<file jq -r '["name", "version"] as $cols | $cols, (.[] | [.[$cols[]]]) | @csv'

Or import them dynamically, e.g. using --args:

<file jq -r '$ARGS.positional, (.[] | [.[$ARGS.positional[]]]) | @csv' \
  --args name version

Output:

"name","version"
"foo","1.24"
"bar","1.21"
"boo","1.23"
"far","1.24"
pmf
  • 24,478
  • 2
  • 22
  • 31