1

How can I (generically) transform the input file below to the output file below, using jq. The record format of the output file is: array_index | key | value

Input file:

[{"a": 1, "b": 10},
 {"a": 2, "d": "fred", "e": 30}]

Output File:

0|a|1
0|b|10
1|a|2
1|d|fred
1|e|30

3 Answers 3

2

Here's a solution using tostream, which creates a stream of paths and their values. Filter out those having values using select, flatten to align both, and join for the output format:

jq -r 'tostream | select(has(1)) | flatten | join("|")'
0|a|1
0|b|10
1|a|2
1|d|fred
1|e|30

Demo

Or a very similar one using paths to get the paths, scalars for the filter, and getpath for the corresponding value:

jq -r 'paths(scalars) as $p | [$p[], getpath($p)] | join("|")'
0|a|1
0|b|10
1|a|2
1|d|fred
1|e|30

Demo

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

Comments

0
< file.json jq -r 'to_entries
                   | .[] 
                   | .key as $k
                   | ((.value | to_entries )[]
                      | [$k, .key, .value])
                   | @csv'

Output:

0,"a",1
0,"b",10
1,"a",2
1,"d","fred"
1,"e",30

You just need to remove the double quotes.

Comments

0

to_entries can be used to loop over the elements of arrays and objects in a way that gives both the key (index) and the value of the element.

jq -r '
   to_entries[] |
   .key as $id |
   .value |
   to_entries[] |
   [ $id, .key, .value ] |
   join("|")
'

Demo on jqplay

Replace join("|") with @csv to get proper CSV.

Comments

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.