【发布时间】:2018-06-05 09:12:21
【问题描述】:
我有一大组数据,我正在使用 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'}
输出这个非 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'}
添加 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})]'
【问题讨论】:
标签: arrays json bash filtering jq