I have a file which has multiple individual JSON arrays, which I want to combine (and remove empty arrays) into a single JSON array
Input
[]
[]
[
[
[
"asdfsdfsdf",
"CCsdfnceR1",
"running",
"us-east-1a",
"34.6X.7X.2X",
"10.75.170.118"
]
]
]
[]
[]
[
[
[
"tyutyut",
"CENTOS-BASE",
"stopped",
"us-west-2b",
null,
"10.87.159.249"
]
],
[
[
"tyutyut",
"dfgdfg-TEST",
"stopped",
"us-west-2b",
"54.2X.8.X8",
"10.87.159.247"
]
]
]
Required output
[
[
"asdfsdfsdf",
"CCsdfnceR1",
"running",
"us-east-1a",
"34.6X.7X.2X",
"10.75.170.118"
],
[
"tyutyut",
"CENTOS-BASE",
"stopped",
"us-west-2b",
null,
"10.87.159.249"
],
[
"tyutyut",
"dfgdfg-TEST",
"stopped",
"us-west-2b",
"54.2X.8.X8",
"10.87.159.247"
]
]
I have a file which has multiple individual JSON arrays, which I want to combine (and remove empty arrays) into a single JSON array
Thanks in advance
2
Answers
This selects only non-empty arrays none of whose elements is an array, and puts them into an array:
The exact requirements aren’t too clear to me but using the following
def
produces the expected result and might be of interest as it is recursive:With this
def
, and the following “main” program:an invocation of jq using the -n option produces the expected result.