'merge multiple lists with jq into single list
I have a aws cli bash loop generating multiple json lists, containing dictionaries. I can't get jq to merge the lists into one large list with all the dictionaries.
e.g.
[ { "key1": "value1", "key2": "value2" },
{ "key1": "value3", "key2": "value4" }
]
[ { "key1": "value5", "key2": "value6" }
]
[ { "key1": "value7" }
]
this gets piped to jq and i would like it to be merged to
[ { "key1": "value1", "key2": "value2" },
{ "key1": "value3", "key2": "value4" },
{ "key1": "value5", "key2": "value6" },
{ "key1": "value7" }
]
Solution 1:[1]
Assuming you have jq 1.5, the solution posted here:
JQ How to merge multiple objects into one
provides the basic technique of using jq -n with the inputs operator.
cat FILE | jq -n '[inputs|.[]]'
does the trick. [inputs] glues the multiple results into a single list and the '.[]' strips the extra level of list that was added.
Solution 2:[2]
i hacked a fix using awk, there must me a way to do it with jq.
| awk 'NR==1{print}NR>1{sub(/^]/,"");sub(/^\[/,",");print}END{print "]"}' \
What it does,
- skip first line "[" (leave it in place)
- remove all "^]" at start of lines
- replace "[" at start of line with "," new list starting
- at the end close it all with "]"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | J Quinn |
| Solution 2 |
