Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

in jq, how to merge an array of object into 1 object with an array of values

$
0
0

Let's say I have this json:

[  {"first": 12355,"second": "abc"  },  {"first": 89010,"second": "def"  },  {"first": 23423,"second": "hij"  },  {"first": 23456,"second": "klm"  },  {"first": 11111,"second": "nop"  }]

And I would like (a generic form) that merges them into 1 object where the value of each key is merged into an array of the corresponding values:

{"first" : [12355,89010,23423,23456,11111],"second" : ["abc","def","hij","klm","nop"]}

I was trying this, but it produces no output at all.

reduce .[] as $final (    {};    ((. | keys) as $k |        map(            ( (.[$k] // []) += ($final[$k] // []))        )    ))

Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>