Home > Mobile >  Combine a LOT of JSON Files Into a Single One
Combine a LOT of JSON Files Into a Single One

Time:11-22

I need to combine a lot of JSON Files into one .json

I have tried it with jq but it will not be in the format that I need. Maybe there is a option with jq that I have missed.

All the JSONS have the following structure.

{"skuReference":"1","quantity":"0","backlog":"0"}

The Output should be:

{"skuReference":"1","quantity":"0","backlog":"0"},
{"skuReference":"2","quantity":"0","backlog":"0"},
{"skuReference":"3","quantity":"0","backlog":"0"},
...

After every "single json" there should be a comma, to separate.

CodePudding user response:

Using sed:

$ sed '$!s/$/,/' files*  # > out_file

It replaces the end-of-line (s/$/) with a comma for all but the last line ($!). So, if I got:

$ cat foo
{"skuReference":"1","quantity":"0","backlog":"0"}
$ cat bar
{"skuReference":"2","quantity":"0","backlog":"0"}

and I:

$ sed '$!s/$/,/' foo bar
{"skuReference":"1","quantity":"0","backlog":"0"},
{"skuReference":"2","quantity":"0","backlog":"0"}

CodePudding user response:

How about this?

jq -s '[.[]]' *.json | tail -n  2 | head -n -1 | output.json

the head and tail are to remove the leading and trailing brackets, since jq will merge into a array type

  • Related