json - 使用 JQ 将同一文件中的多个 JSON 数组合并为一个 JSON 数组
问题描述
我有一个文件,其中包含多个单独的 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
推荐阅读
- html - 当文本对齐时,某些单词之间的间距在移动视图中太大了
- c# - 使用 lambda 表达式获取子属性实例的路径
- ssh - 阿帕奇气流 | 如何在使用 SSHOperator 时检查来自命令的响应?
- php - PHP仅针对经过身份验证的用户提交到数据库表
- node.js - 如何修复“致命错误:堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足”错误
- python - 加速 Pandas:查找满足条件集的所有列
- php - 如何在sql查询中调整时间戳?
- python - 将 html/xml 中的 soap API 数据“suds.sax.text.Text”传输到 json
- javascript - node.js / nuxt 中变量前的“+”
- r - 绘制交叉表信息