1

I have several json files I want to combine. Some are arrays of objects and some are single objects. I want to effectively concatenate all of this into a single array.

For example:

[ { "name": "file1" } ] 
{ "name": "file2" } 
{ "name": "file3" } 

And I want to end up with:

[ { "name": "file1" } { "name": "file2" }, { "name": "file3" }, ] 

How can I do this using jq or similar?

1
  • 1
    I believe this does it: jq -n '[inputs] | add' $(find . -name '*.json') > combined.json Commented Mar 2, 2021 at 16:33

2 Answers 2

1

The following illustrates an efficient way to accomplish the required task:

jq -n 'reduce inputs as $in (null; . + if $in|type == "array" then $in else [$in] end) ' $(find . -name '*.json') > combined.json 

The -n command-line option is necessary to avoid skipping the first file.

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

Comments

0

This did it:

jq -n '[inputs] | add' $(find . -name '*.json') > combined.json

6 Comments

Alas, this is incorrect, and also an inefficient way to do what it does.
Can you offer context on why this is 'incorrect'? It seems to work, what is incorrect about it?
Your requirements differentiate between files with an array and those with a top-level JSON object. Have you tried your own example? Please also compare with my answer.
Yes I have. I have a resulting file that is a single array where all inner types have been flattened. I have experienced no issues so far. I'm not sure how I would 'compare'. I would expect both to give me what I described. Unless there's a counterexample (or you can give me some details on why one is 'incorrect', which I'd genuinely appreciate) I'm not sure what I'm comparing. Why is your answer superior? (Asking out of genuine ignorance.)
I meant compare the logic. I used your example with three files.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.