Closed gustaff-weldon closed 11 years ago
I'm assuming you meant {"key1": "value1", "key2": "value2"}
as the desired output (can't repeat keys in JSON).
jq's +
and add
functions (+ takes two arguments, add
takes an array and adds them all up) can merge objects. So, you first need to convert it into a list of objects with one key-value entry each, then add them together.
jq 'map({(.id): .value}) | add'
Yes, sorry for the copy-paste error, fixed that one. Thanks, for the answer.
My scenario is slightly different. Keys are dynamic
[ { "100": "ONE", "200": "TWO" }, { "100": "1", "200": "2" } ]
My expected output is{"1":"ONE","2":"TWO"}
. How can I write an jq expresssion
Is there way to to take an array of objects and produce one object as a result? input:
And I would like to transform this output into
I can get a set of objects by using
but that's not what I want.
Is there a way to use
+
to merge with last result? Or just to back-reference last result somehow? Or maybe a{}
syntax similar to creating an array by surrounding whole expression eg.I have also tried abusing
=
operator (jq ".[] | (.id)= .value "
, but it did not yield usable results. Any help would be appreciated.