Search code examples
jsonjqtrivy

Exclude null inputs while concatenating JSON files


I'm looking to concatenate multiple JSON files in a directory using jq. Majority of the JSON files look like this

[
  {
    a: 12,
    b: 22,
    c: []
  }
]

So for concatenating them I use the follow jq query

jq -s '[.[]|.[ ]]' *.json > xyz.json

This seems work okay and I get the expected concatenated output until it encounters a JSON file that just has null written in it, and when I try to concatenate this with the others I get an error.

$ cat test.json
  null
$ jq -s '[.[]|.[ ]]' xyz.json test.json > y.json
jq: error (at test.json:0): Cannot iterate over null (null)

Is there a way to exclude this null JSON file while concatenating through the directory?

I must mention these JSON files are generated as outputs for different Trivy Image scans and I have no control over what the output is.

If anyone else has faced this issue previously please help me out.


Solution

  • Just select array inputs.

    jq -n '[inputs | arrays[]]' *.json
    

    Online demo

    Use select(. != null) instead of arrays if you don't want to exclude all non-arrays but only null.