document有数组怎么解决JQ处理为多个字典?
How to solve JQ processing as multiple dictionaries if document has an array?
我有以下 JSON 文档
[
{
"description": "Component 1",
"tags": [
"abc",
"123",
"xyz"
]
},
{
"description": "Component 2",
"tags": [
"def",
"foo",
"bar"
]
},
{
"description": "Component 3",
"tags": [
"def"
]
}
]
我在下面写了JQ过滤器来解析JSON,预期的输出不是生成多个字典。
{ (.[].description):(.[].tags)}
应用过滤器后,由于 tags
数组
,我使用 multiple dictionaries
得到以下输出
{
"Component 1": [
"abc",
"123",
"xyz"
]
}
{
"Component 1": [
"def",
"foo",
"bar"
]
}
{
"Component 1": [
"def"
]
}
{
"Component 2": [
"abc",
"123",
"xyz"
]
}
{
"Component 2": [
"def",
"foo",
"bar"
]
}
{
"Component 2": [
"def"
]
}
{
"Component 3": [
"abc",
"123",
"xyz"
]
}
{
"Component 3": [
"def",
"foo",
"bar"
]
}
{
"Component 3": [
"def"
]
}
我期望的输出没有 multiple dictionaries
,如下所示
{
"Component 1": [
"abc",
"123",
"xyz"
]
}
{
"Component 2": [
"def",
"foo",
"bar"
]
}
{
"Component 3": [
"def"
]
}
如何使用 JQ 生成上述预期的输出?
你可以转换成这个
jq '.[] | { (.description) : .tags }'
为了摆脱多次出现
我有以下 JSON 文档
[
{
"description": "Component 1",
"tags": [
"abc",
"123",
"xyz"
]
},
{
"description": "Component 2",
"tags": [
"def",
"foo",
"bar"
]
},
{
"description": "Component 3",
"tags": [
"def"
]
}
]
我在下面写了JQ过滤器来解析JSON,预期的输出不是生成多个字典。
{ (.[].description):(.[].tags)}
应用过滤器后,由于 tags
数组
multiple dictionaries
得到以下输出
{
"Component 1": [
"abc",
"123",
"xyz"
]
}
{
"Component 1": [
"def",
"foo",
"bar"
]
}
{
"Component 1": [
"def"
]
}
{
"Component 2": [
"abc",
"123",
"xyz"
]
}
{
"Component 2": [
"def",
"foo",
"bar"
]
}
{
"Component 2": [
"def"
]
}
{
"Component 3": [
"abc",
"123",
"xyz"
]
}
{
"Component 3": [
"def",
"foo",
"bar"
]
}
{
"Component 3": [
"def"
]
}
我期望的输出没有 multiple dictionaries
,如下所示
{
"Component 1": [
"abc",
"123",
"xyz"
]
}
{
"Component 2": [
"def",
"foo",
"bar"
]
}
{
"Component 3": [
"def"
]
}
如何使用 JQ 生成上述预期的输出?
你可以转换成这个
jq '.[] | { (.description) : .tags }'
为了摆脱多次出现