请随意编辑标题,我不知道如何最好地表达它:)。
例如,我有一个 JSON,它看起来像这样:
{
"things": [
{
"name": "foo",
"params": [
{
"type": "t1",
"key": "key1",
"value": "val1"
},
{
"type": "t1",
"key": "category",
"value": "thefoocategory"
}
]
},
{
"name": "bar",
"params": [
{
"type": "t1",
"key": "key1",
"value": "val1"
},
{
"type": "t1",
"key": "category",
"value": "thebarcategory"
}
]
}
]
}
我想要实现的是输出看起来像
[
{
name: "foo",
category: "thefoocategory"
},
{
name: "bar",
category: "thebarcategory"
}
]
我可以很容易地使用
jq ' .things | .[] | .name'
提取名称我还可以使用
jq ' .things | .[] | .params | .[] | select(.key == "category") | .value'
提取类别但我无法将它们结合起来。
任何帮助表示赞赏
最佳答案
这实际上是相对简单的:
.things | .[] | {name: .name, category: .params | .[] | select(.key=="category") | .value }
关于json - 如何使用 jq 从子数组创建具有任意键的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37864617/