我有一个包含多个单独的JSON数组的文件,我想将其合并(并删除空数组)为单个JSON数组
输入
[]
[]
[
[
[
"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"
]
]
]
必需的输出
[
[
"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"
]
]
我有一个包含多个单独的JSON数组的文件,我想将其合并(并删除空数组)为单个JSON数组
提前感谢
这仅选择非空数组,其元素都不是数组,并将它们放入数组:
jq -n '[ inputs | .. | select(type=="array" and .!=[] and all(.[]; type!="array")) ]' file
确切的要求对我来说还不太清楚,但是使用以下def
会产生预期的结果,并且可能是递归的:
def peel:
if type == "array"
then if length == 0 then empty
elif length == 1 and (.[0] | type) == "array" then .[0] | peel
elif all(.[]; type=="array") then .[] | peel
else [.[] | peel]
end
else .
end;
使用此def
和以下“主”程序:
[inputs | peel]
使用-n选项调用jq会产生预期的结果。