【发布时间】:2021-12-17 16:29:01
【问题描述】:
我正在使用 JQ CLI 将 JSON 从文档合并到另一个文档。我面临的问题是我选择了属性的值,而不是数字数组索引
第一个文件包含一段 JSON jqtest.json:
{
"event": [
{
"listen": "test",
"script": {
"exec": [],
"type": "text/javascript"
}
}
]
}
第二个文件是我想将 JSON 合并到“accounts”collection.json 下的地方:
{
"item": [
{
"name": "accounts",
"item": [
{
"name": "Retrieves the collection of Account resources."
}
]
},
{
"name": "accounts mapped",
"item": [
{
"name": "Retrieves the collection of AccountMapped resources."
}
]
}
]
}
我要做的是将它合并到“帐户”和“名称”下:“检索帐户资源的集合。”我使用命令:
jq -s '
.[0].event += .[1].item |
map(select(.name=="accounts")) |
.[].item
' jqtest.json collection.json
但是执行时什么都没有输出。 JQ 有什么问题,或者我可以使用其他工具来完成此操作吗?
{
"item": [
{
"name": "accounts",
"item": [
{
"name": "Retrieves the collection of Account resources.",
"event": [
{
"listen": "test",
"script": {
"exec": [],
"type": "text/javascript"
}
}
]
},
{
"name": "accounts mapped",
"item": [
{
"name": "Retrieves the collection of AccountMapped resources."
}
]
}
]
}
]
}
【问题讨论】:
-
您能否提供一个您期望的输出示例?不清楚是什么 不清楚«merge it under "accounts"» 是什么意思,不清楚«merge it under "accounts" and under "name"» 是什么意思,不清楚你想要这两个中的哪一个。
-
这不是有效的 JSON。
-
我编辑了上面的问题并在上面添加了所需的输出
-
抱歉,我没有注意到编辑。 (我没有收到通知是因为你没有在评论中使用
@ikegami和/或因为我圣诞节太忙了。)