zbeedatm
zbeedatm

Reputation: 679

bashscript combine multiple json files into one json

I have a folder that contains subfolders of json files inside.

I need to write a bash script that combine all json files into one big valid json.

1) Tried first to use jq to combine first, all json files into each directory and later on I'll need to combine all into one big file again. I didn't manage to make it work. I used this command:

jq -rs 'reduce .[] as $item ({}; . * $item)'

2) Other option is to create a json file at the beginning with "[" --> Process all files from all directories and for each file append the content --> append "]" at the end.

Can I achieve the same result with first way using jq only?

Upvotes: 5

Views: 6881

Answers (1)

EchoMike444
EchoMike444

Reputation: 1692

a very simple way is :

jq -s 'flatten' $target/*/*.json > $merged_json

a alternative ( in the case you need to use | ) :

cat $target/*/*.json | jq -s 'flatten' > $merged_json

or if too many files

find $target/* -name \*json cat {} |  jq -s 'flatten' > $merged_json

Upvotes: 10

Related Questions