5

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" }
]
4
  • 2
    The snippet is not valid JSON. Properly quote the values in the input shown Commented Aug 13, 2020 at 10:42
  • No, it isn't - jsonlint.com, try pasting it here Commented Aug 13, 2020 at 11:17
  • 3
    jq -s add filename works fine. Commented Aug 13, 2020 at 11:20
  • I checked it there, the 2nd bit (result) is valid, the first is just 3 json lists, that is the issue. The bash loop runs aws cli and gets a new json list back everytime, i am trying to merge them into one using jq Commented Aug 13, 2020 at 11:22

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

-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,

  1. skip first line "[" (leave it in place)
  2. remove all "^]" at start of lines
  3. replace "[" at start of line with "," new list starting
  4. at the end close it all with "]"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.