英文:
Concatenate nested JSON structures using jq
问题
以下是翻译好的部分:
{
"items": [
{
"name": "test",
"description": "description",
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
},
{
"name": "test2",
"description": "description2",
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
]
}
英文:
Edit: updated question as example was too simplified to cover specific case:
Suppose I have a json object:
{
"items": [
{
"details": {
"name": "test",
"description": "description"
},
"properties": [
{
"property1": "someValue",
"property2": "someOtherValue"
}
],
"additionalProperties": {
"other": "stuff"
}
},
{
"details": {
"name": "test2",
"description": "description2"
},
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
],
"additionalProperties": {
"other": "stuff"
}
}
]
}
I would like to extract the two json objects details
and properties
and and insert into item
, replacing all other properties. In this example there are additionalProperties
that should be ignored, but there could be up to n
other properties that should also be ignored.
Desired output:
{
"items": [
{
"name": "test",
"description": "description",
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
},
{
"name": "test2",
"description": "description2",
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
]
}
I have tried a few different ways using map - but I'm not sure if this is the correct approach. I've seen similar solutions involving reduce
too, however I am yet to get something that works. Any help would be appreciated!
答案1
得分: 2
Update |=
each item in the .items
array by adding up .details
and .properties
:
jq '.items[] |= .details + .properties'
{
"items": [
{
"name": "test",
"description": "description",
"property1": "someValue",
"property2": "someOtherValue"
},
{
"name": "test2",
"description": "description2",
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
Edit: With the updated input files, just add {properties}
as is (replacing .properties
):
jq '.items[] |= .details + {properties}'
{
"items": [
{
"name": "test",
"description": "description",
"properties": [
{
"property1": "someValue",
"property2": "someOtherValue"
}
]
},
{
"name": "test2",
"description": "description2",
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
]
}
英文:
Update |=
each item in the .items
array by adding up .details
and .properties
:
jq '.items[] |= .details + .properties'
{
"items": [
{
"name": "test",
"description": "description",
"property1": "someValue",
"property2": "someOtherValue"
},
{
"name": "test2",
"description": "description2",
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
Edit: With the updated input files, just add {properties}
as is (replacing .properties
):
jq '.items[] |= .details + {properties}'
{
"items": [
{
"name": "test",
"description": "description",
"properties": [
{
"property1": "someValue",
"property2": "someOtherValue"
}
]
},
{
"name": "test2",
"description": "description2",
"properties": [
{
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
]
}
答案2
得分: 0
使用 *
运算符:
jq '.items[] |= (.details * .properties[])' test.json
{
"items": [
{
"name": "test",
"description": "description",
"property1": "someValue",
"property2": "someOtherValue"
},
{
"name": "test2",
"description": "description2",
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
英文:
With *
operator:
jq '.items[] |= (.details * .properties[])' test.json
{
"items": [
{
"name": "test",
"description": "description",
"property1": "someValue",
"property2": "someOtherValue"
},
{
"name": "test2",
"description": "description2",
"property1": "someValue2",
"property2": "someOtherValue2"
}
]
}
答案3
得分: 0
我已成功通过以下方式获得所需的输出:
jq '.items[] |= .details + {"properties": .properties}'
理想情况下,我不想指定键“properties”,但尚未找到其他方法。
英文:
I have managed to get the desired output with the following:
jq '.items[] |= .details + {"properties": .properties}'
ideally I don't want to specify the key "properties" but am yet to find another way
答案4
得分: 0
有许多可能的解决方案,正如现有的出色答案所证明的那样。其中一种解决方案采用了一种具有建设性但略显重复的方法:
{
items: .items | map({
name: .details.name,
description: .details.description,
properties
})
}
类似地,您可以重新构建数组的项,然后进行更新赋值:
.items[] |= {
name: .details.name,
description: .details.description,
properties
}
英文:
There are many possible solutions as evidenced by the existing great answers. One solution takes the constructive, albeit slightly repetetive, approach:
{
items: .items | map({
name: .details.name,
description: .details.description,
properties
})
}
Similarly, you can re-construct the items of the array and then update-assign them:
.items[] |= {
name: .details.name,
description: .details.description,
properties
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论