2

I have a JSON file containing application clients and their associated application features:

{
    "client-A": [
        "feature-x"
    ],
    "client-B": [
        "feature-x",
        "feature-y"
    ],
    "client-C": [
        "feature-z"
    ],
    "client-D": [
        "feature-x",
        "feature-z"
    ],
    ...
}

I'm trying to turn this into the following CSV:

client,feature
client-A,feature-x
client-B,feature-x
client-B,feature-y
client-C,feature-z
client-D,feature-x
client-D,feature-z

What's an easy way using jq to get this done?

1 Answer 1

3

Not sure whether this is the most efficient way of doing it, but you can convert use the following pipeline:

<yourfile.json jq -r 'to_entries | .[] | { key: .key, value: .value[] } | [ .key, .value ] | @csv'

to_entries converts the structure into "key value" pairs, which can then be operated on. The { key: .key, value: .value[] } bit will convert the array into multiple rows...

Sign up to request clarification or add additional context in comments.

1 Comment

The following would be slightly more efficient: to_entries[] | [.key] + (.value[]|[.]) | @csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.