I want to merge to arrays together.
$ cat file.json
[1,2,3,4]
[5,6,7,8]
$ # command
[1,2,3,4,5,6,7,8]
What should # command be?
CodePudding user response:
Another simple way, using just add:
jq -s 'add' input.json
JqPlay Demo
Local shell example
$ cat input.json [1,2,3,4] [5,6,7,8] $ $ jq -s 'add' input.json [ 1, 2, 3, 4, 5, 6, 7, 8 ] $
CodePudding user response:
jq -s 'flatten(1)' file.json
Explanation:
flatten(1)de-nests arrays with depth of 1.-sruns the command on all the items (ie. both lists) rather than each item independently.
CodePudding user response:
Yet another way
jq -n '[inputs[]]' file.json
CodePudding user response:
The c option prints to one line.
Also if you want to merge the two arrays and keep them sorted you can do this:
jq -cn '[inputs[]] | sort' file.json
For example, it will still be in order in the case below:
$ cat file.json
[1,2,3,5]
[4,6,7,8]
$ jq -cn '[inputs[]] | sort ' file.json
[1,2,3,4,5,6,7,8]
