鉴于此输入
{
"data": [
{
"id": "001",
"metadata": {
"item1": 1
},
"records": [
{
"name": "name1",
"type": "type1"
},
{
"name": "name2",
"type": "type2"
}
]
},
{
"id": "002",
"metadata": {
"item1": 1
},
"records": [
{
"name": "name1",
"type": "type1"
}
]
},
{
"id": "003",
"metadata": {},
"records": [
{
"name": "name1",
"type": "type1"
},
{
"name": "name2",
"type": "type2"
}
]
}
]
}
我想输出这个
[
{
"id": "001",
"Item1": 1,
"Name": "name2"
},
{
"id": "002",
"Item1": 1,
"Name": null
},
{
"id": "003",
"Item1": null,
"Name": "name2"
}
]
然而使用此
jq '[.data[] | {id, "Item1": .metadata.item1, "Name": .records[] | select(.type == "type2").name}]'
jq查询我得到了这个
[
{
"id": "001",
"Item1": 1,
"Name": "name2"
},
{
"id": "003",
"Item1": null,
"Name": "name2"
}
]
我怎样才能获得' 002' id对象也输出?我已经尝试了各种if if else条件语句,但无济于事。
答案 0 :(得分:1)
我对map
很新,所以这可能不是最佳解决方案。
如果select
将[
.data[] |
{
id : .id,
Item1 : .metadata.item1,
Name : .records | map(select(.type == "type2"))[0].name
}
]
改为记录数组,则不会删除周围的对象。例如:
parse.jq
jq -f parse.jq file.json
像这样运行:
[
{
"id": "001",
"Item1": 1,
"Name": "type2"
},
{
"id": "002",
"Item1": 1,
"Name": null
},
{
"id": "003",
"Item1": null,
"Name": "type2"
}
]
输出:
Volunteers:
VID - ID of volunteer
ActiveGroups - Multivalue list of VolunteerGroups that they're active in
InterestGroupss - Multuvalue list of VolunteerGroups that they are interested in
VolunteerGroups:
ID - ID of group
GroupName - name of group
答案 1 :(得分:0)
可能是因为第二个嵌套数组不满足条件
select(.type == "type2")