我有以下要合并的 JSON 对象。所以场景是
ticketId
并且 requester_id
应该成为 componentsByProdTourId
数组中每个对象的属性。
这是输入的 JSON 数据。
{
"componentsByProdTourId": [
{
"region": "Europe",
"compSpec": {
"country": "France",
"attributes": [
{
"attributeCode": "ABC",
"attributeType": "xyx"
}
]
}
},
{
"region": "Europe",
"compSpec": {
"country": "France",
"attributes": [
{
"attributeCode": "EFG",
"attributeType": "lmn"
}
]
}
},
{
"region": "Europe",
"compSpec": {
"country": "United Kingdom",
"attributes": [
{
"attributeCode": "FLC",
"attributeType": "omp"
}
]
}
}
],
"ticketData": {
"ticketId": "1234",
"requester_id": "99885"
}
}
我正在寻找的理想结果:
{
"componentsByProdTourId": [
{
"region": "Europe",
"compSpec": {
"country": "France",
"attributes": [
{
"attributeCode": "ABC",
"attributeType": "xyx"
}
]
},
"ticketId": 1234,
"requester_id": 99885
},
{
"region": "Europe",
"compSpec": {
"country": "France",
"attributes": [
{
"attributeCode": "EFG",
"attributeType": "lmn"
}
]
},
"ticketId": 1234,
"requester_id": 99885
},
{
"region": "Europe",
"compSpec": {
"country": "United Kingdom",
"attributes": [
{
"attributeCode": "FLC",
"attributeType": "omp"
}
]
},
"ticketId": 1234,
"requester_id": 99885
}
],
"ticketData": {
"ticketId": "1234",
"requester_id": "99885"
}
}
提前致谢。
我尝试了这个 Jq 命令:
.componentsByProdTourId |= map(. + {ticketId: .ticketData.ticketId, requester_id: .ticketData.requester_id})
但结果并不理想。
ticketId
和 requester_id
为空。这是输出:
{
"componentsByProdTourId": [
{
"region": "Europe",
"compSpec": {
"country": "France",
"attributes": [
{
"attributeCode": "ABC",
"attributeType": "xyx"
}
]
},
"ticketId": null,
"requester_id": null
},
{
"region": "Europe",
"compSpec": {
"country": "France",
"attributes": [
{
"attributeCode": "EFG",
"attributeType": "lmn"
}
]
},
"ticketId": null,
"requester_id": null
},
{
"region": "Europe",
"compSpec": {
"country": "United Kingdom",
"attributes": [
{
"attributeCode": "FLC",
"attributeType": "omp"
}
]
},
"ticketId": null,
"requester_id": null
}
],
"ticketData": {
"ticketId": "1234",
"requester_id": "99885"
}
}
.componentsByProdTourId[] += .ticketData
就够了。您的版本失败,因为当您尝试访问
.ticketData
时,.
是您要映射的项目,而不是根对象。您可以通过使用 as
捕获 .ticketData
来解决此问题,但有一种更简单的方法,即完全跳过 map
。