[英]JQ filtering on fields in nested Objects
我有大量数据,我正在使用JQ构造仅包含我感兴趣的数据作为记录的对象。 我的问题是我开始看到重复的对象,看来我的语法不正确。
我正在使用一个包含平面字段和子对象数组的对象,有些特定的字段我想提取出来并制作具有所有所需数据的新对象。 包括一些平面字段和一些来自数组对象的字段。
这是一个较小的示例,有助于演示问题tmpData.json
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batter": [{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
{
"id": "1003",
"type": "Blueberry"
},
{
"id": "1004",
"type": "Devil's Food"
}
]
}
我运行这个: cat tmpData.txt | jq {'id: .id, type: .type, batter: .batter[].id'}
cat tmpData.txt | jq {'id: .id, type: .type, batter: .batter[].id'}
它将输出此非json对象集(缺少逗号)
{
"id": "0001",
"type": "donut",
"batter": "1001"
}
{
"id": "0001",
"type": "donut",
"batter": "1002"
}
{
"id": "0001",
"type": "donut",
"batter": "1003"
}
{
"id": "0001",
"type": "donut",
"batter": "1004"
}
很好 我现在有一些对象,每个对象都包含parentID 0001
并且数组中的不同项在每个对象中都有关联。
当我运行时: cat tmpData.txt | jq {'id: .id, type: .type, batterID: .batter[].id, batterType: .batter[].type'}
cat tmpData.txt | jq {'id: .id, type: .type, batterID: .batter[].id, batterType: .batter[].type'}
使用添加的type
字段,我得到了很多重复的错误地关联项目
{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Regular"
}
{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Chocolate"
}
{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Blueberry"
}
{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Devil's Food"
}
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Regular"
}
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Chocolate"
}
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Blueberry"
}
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Devil's Food"
}
{
"id": "0001",
"type": "donut",
"batterID": "1003",
"batterType": "Regular"
}
{
"id": "0001",
"type": "donut",
"batterID": "1003",
"batterType": "Chocolate"
}
{
"id": "0001",
"type": "donut",
"batterID": "1003",
"batterType": "Blueberry"
}
{
"id": "0001",
"type": "donut",
"batterID": "1003",
"batterType": "Devil's Food"
}
{
"id": "0001",
"type": "donut",
"batterID": "1004",
"batterType": "Regular"
}
{
"id": "0001",
"type": "donut",
"batterID": "1004",
"batterType": "Chocolate"
}
{
"id": "0001",
"type": "donut",
"batterID": "1004",
"batterType": "Blueberry"
}
{
"id": "0001",
"type": "donut",
"batterID": "1004",
"batterType": "Devil's Food"
}
现在,我看到每个batterID
都存在于具有每种regular, chocolate, blueberry
类型的对象中。 但实际上1002
只是chocolate
。
我理想的输出是这样的
[{
"id": "0001",
"type": "donut",
"batterID": "1001",
"batterType": "Regular"
},
{
"id": "0001",
"type": "donut",
"batterID": "1002",
"batterType": "Chocolate"
}]
感谢您的专业知识!
编辑已解决:工作命令: cat tmpData.txt | jq '[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]'
cat tmpData.txt | jq '[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]'
{id: id, type: .type}
缩写为{id, type}
将所有内容放在一起:
[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.