json - 将 JSON 转换为紧凑格式
问题描述
我正在尝试转换以下 json
{ "application" : [
{ "name" : "app1",
"policies" : [
{ "name" : "pol_1",
"orderNumber" : "10"
},
{ "name" : "pol_2",
"orderNumber" : "20"
}
]
},
{ "name" : "app2",
"policies" : [
{ "name" : "pol_A",
"orderNumber" : "10"
},
{ "name" : "pol_B",
"orderNumber" : "20"
}
]
}
]
}
到以下
{ "pol_1":"10", "pol_2":"20" }
使用
jq -r ".application[] | select(.name==\"app1\") | .policies[] | {\".name\" : .orderNumber}"
我能够得到
{
"pol_1":"10"
}
{
"pol_2":"20"
}
知道如何合并它们。我错过了什么还是我做错了?
解决方案
你快到了。用于map
创建单个数组而不是两个独立对象,然后用于add
合并其内容。
jq '.application[]
| select(.name == "app1")
| .policies
| map({ (.name) : .orderNumber } )
| add' file.json
推荐阅读
- menu - 在 SwiftUI 中创建菜单 - 接收 [UILog] Called -[UIContextMenuInteraction updateVisibleMenuWithBlock:] 的输出消息
- javascript - Angular 10 测试:组件解析器数据订阅错误
- swift - 从 UINavigationController 扩展设置导航项
- django - 我有一个错误 Dockerfile 与 Django 。如何解决?
- php - 使用 PDO 将数据提取到表中导致没有值
- excel - 如何跨两个潜艇使用范围变量?
- python - sns.pairplot 为 Kmeans 集群可视化返回错误的结果
- twilio-api - 如何在 android 和 IOS 中检索 Twilio 我的一对一私人聊天频道列表
- latex - 如何使用 pandoc+beamer 从 toc 标题中删除数字
- javascript - 将多个 PannerNode 连接到输出