Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq flatten list of objects into one object

Tags:

jq

I want to go from

[
  {"key_skjdghkbs": "deep house"},
  {"key_kjsskjbgs": "deadmau5"},
  {"key_jhw98w4hl": "progressive house"},
  {"key_sjkh348vg": "swedish house mafia"},
  {"key_js3485jwh": "dubstep"},
  {"key_jsg587jhs": "escape"}
]

to

{
  "key_skjdghkbs": "deep house",
  "key_kjsskjbgs": "deadmau5"
  "key_jhw98w4hl": "progressive house",
  "key_sjkh348vg": "swedish house mafia",
  "key_js3485jwh": "dubstep",
  "key_jsg587jhs": "escape"
}

Each object in the original list has exactly one key but the keys are unique.

I could do something like jq .[] .genre if the keys were the same but they're not.

like image 399
abattleofwords Avatar asked Dec 30 '25 16:12

abattleofwords


1 Answers

jq's add function does exactly this

jq 'add'
like image 168
abattleofwords Avatar answered Jan 02 '26 18:01

abattleofwords



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!