Reducing JSON with jq

Tags: unix jq
By : turtle

I have a JSON array of Objects:

[{key1: value},{key2:value}, ...]

I would like to reduce these into the following structure:

{key1: value, key2: value, ...} 

Is this possible to do with jq?

I was trying:

cat myjson.json | jq '.[] | {(.key): value}'

This doesn't quite work as it iterates over each datum rather than reducing it to one Object.

By : turtle


I believe the following will work:

cat myjson.json | jq 'reduce .[] as $item ({}; .   $item)'

It takes each item in the array, and adds it to the sum of all the previous items.

To expand on the other two answers a bit, you can "add" two objects together like this:

.[0]   .[1]


This video can help you solving your question :)
By: admin