This is related to my previous question: How to use jq to format array of objects to separated list of key values
How can I (generically) transform the input file below to the output file below, using jq. The value at key "id" uniqely identifies the array element. The record format of the output file is: (value at key "id") | key | value.
I can do this if I add awk to solution of previous question, but I am having trouble getting my head around doing it all in jq.
Input file:
[{"id": 11, "b": 100},
{"id": 12, "d": "fred", "e": 300}]
Output File:
11|id|11
11|b|100
12|id|12
12|d|fred
12|e|300
CodePudding user response:
Here's a solution using to_entries
, which decomposes an object into an array of key-value pairs:
jq -r '.[] | .id as $id | to_entries[] | [$id,.key,.value] | join("|")'
11|id|11
11|b|100
12|id|12
12|d|fred
12|e|300