0

I need to flatten this json array of flat objects

[{
  "key1a": "value1",
  "key1b": "value2",
  ...
  "key1z": "value50",
},
{
  "key2a": "value2a",
  "key2b": "value2b",
  ...
  "key2y": "value2y",
},
{...},{...}]

to this csv style output

key1a+key1b+key1c... value1a|value1b|value1c...
key2a+key2b+key2c... value2a|value2b|value2c...

in order to do some post processing with awk.

I tried with ja functions map, add and flatten but did not succeed.

0

Does this do the trick for you?

$ cat input.json | jq -r '.[] | to_entries | ([ .[].key ] | join("+")) + " " + ([ .[].value ] | join("|"))'
key1a+key1b+key1z value1a|value2a|value50
key2a+key2b+key2y value2a|value2b|value2y
$ cat input.json
[{
  "key1a": "value1a",
  "key1b": "value2a",
  "key1z": "value50"
},
{
  "key2a": "value2a",
  "key2b": "value2b",
  "key2y": "value2y"
}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.