如何使用 jq 过滤 json 个对象数组?
How can I filter an array of json objects using jq?
我已经走到这一步了。
json 输入,'data.json':
[
{
"Selected": null,
"Family Name": "Jones",
"Couple Name": "Jones, Adam & Rachael Margaret",
"Family Phone": "404-4477",
"Family Email": "email@sbcglobal.net",
"Family Address": "777 Aggies Court Kindly, California 95388 ",
"Head Of House Name": "Jones, Adam",
"Head Of House Phone": "(583) 404-2488",
"Head Of House Email": "email@sbcglobal.net",
"Spouse Name": "Jones, Rachael Margaret",
"Spouse Phone": null,
"Spouse Email": null,
"Child Name": null,
"Child Phone": null,
"Child Email": null
},
{
"Selected": "x",
"Family Name": "Xiong",
"Couple Name": "Xiong, Arlene Frances",
"Family Phone": null,
"Family Email": null,
"Family Address": "888 Walnut Ave. Blatant, California 95388 ",
"Head Of House Name": "Xiong, Arlene Frances",
"Head Of House Phone": "583-500-7917",
"Head Of House Email": "email@junk.net",
"Spouse Name": null,
"Spouse Phone": null,
"Spouse Email": null,
"Child Name": null,
"Child Phone": null,
"Child Email": null
},
{
"Selected": "x",
"Family Name": "Blair",
"Couple Name": "Blair, Toby & Silvia",
"Family Phone": "358-4645",
"Family Email": null,
"Family Address": "333 Cindy St. Stoic, California 95388 ",
"Head Of House Name": "Blair, Toby",
"Head Of House Phone": null,
"Head Of House Email": "email@stuff.net",
"Spouse Name": "Blair, Silvia",
"Spouse Phone": null,
"Spouse Email": null,
"Child Name": null,
"Child Phone": null,
"Child Email": null
}
]
我可以用它来过滤:
cat data.json | jq '.[] | select(.Selected != null) | {"Head Of House Name", "Head Of House Phone", "Head Of House Email", "Family Phone", "Family Email"}'
结果:
{
"Head Of House Name": "Xiong, Arlene Frances",
"Head Of House Phone": "583-500-7917",
"Head Of House Email": "email@junk.net",
"Family Phone": null,
"Family Email": null
}
{
"Head Of House Name": "Blair, Toby",
"Head Of House Phone": null,
"Head Of House Email": "email@stuff.net",
"Family Phone": "358-4645",
"Family Email": null
}
注意对象数组周围没有数组括号,对象之间也没有逗号...表明结果不是数组。
问题是结果不是一个对象数组(坦率地说,我不确定它是什么)。如何从 json 个对象数组开始,过滤它们并以过滤后的对象列表结束?
当您使用过滤器 .[]
时,您实际上是将数组结果分解为每个项目的单独结果。您最终看到的是一个接一个的结果。
为确保将它们保存为数组,您可以通过用方括号括起过滤器将结果放回数组中:
[
.[] |
select(.Selected != null) |
{
"Head Of House Name",
"Head Of House Phone",
"Head Of House Email",
"Family Phone",
"Family Email"
}
]
或者使用 map()
过滤器将其保留为数组,然后您可以删除 .[]
部分:
map(
select(.Selected != null) |
{
"Head Of House Name",
"Head Of House Phone",
"Head Of House Email",
"Family Phone",
"Family Email"
}
)
如果你问我,你应该坚持map()
。
我已经走到这一步了。
json 输入,'data.json':
[
{
"Selected": null,
"Family Name": "Jones",
"Couple Name": "Jones, Adam & Rachael Margaret",
"Family Phone": "404-4477",
"Family Email": "email@sbcglobal.net",
"Family Address": "777 Aggies Court Kindly, California 95388 ",
"Head Of House Name": "Jones, Adam",
"Head Of House Phone": "(583) 404-2488",
"Head Of House Email": "email@sbcglobal.net",
"Spouse Name": "Jones, Rachael Margaret",
"Spouse Phone": null,
"Spouse Email": null,
"Child Name": null,
"Child Phone": null,
"Child Email": null
},
{
"Selected": "x",
"Family Name": "Xiong",
"Couple Name": "Xiong, Arlene Frances",
"Family Phone": null,
"Family Email": null,
"Family Address": "888 Walnut Ave. Blatant, California 95388 ",
"Head Of House Name": "Xiong, Arlene Frances",
"Head Of House Phone": "583-500-7917",
"Head Of House Email": "email@junk.net",
"Spouse Name": null,
"Spouse Phone": null,
"Spouse Email": null,
"Child Name": null,
"Child Phone": null,
"Child Email": null
},
{
"Selected": "x",
"Family Name": "Blair",
"Couple Name": "Blair, Toby & Silvia",
"Family Phone": "358-4645",
"Family Email": null,
"Family Address": "333 Cindy St. Stoic, California 95388 ",
"Head Of House Name": "Blair, Toby",
"Head Of House Phone": null,
"Head Of House Email": "email@stuff.net",
"Spouse Name": "Blair, Silvia",
"Spouse Phone": null,
"Spouse Email": null,
"Child Name": null,
"Child Phone": null,
"Child Email": null
}
]
我可以用它来过滤:
cat data.json | jq '.[] | select(.Selected != null) | {"Head Of House Name", "Head Of House Phone", "Head Of House Email", "Family Phone", "Family Email"}'
结果:
{
"Head Of House Name": "Xiong, Arlene Frances",
"Head Of House Phone": "583-500-7917",
"Head Of House Email": "email@junk.net",
"Family Phone": null,
"Family Email": null
}
{
"Head Of House Name": "Blair, Toby",
"Head Of House Phone": null,
"Head Of House Email": "email@stuff.net",
"Family Phone": "358-4645",
"Family Email": null
}
注意对象数组周围没有数组括号,对象之间也没有逗号...表明结果不是数组。
问题是结果不是一个对象数组(坦率地说,我不确定它是什么)。如何从 json 个对象数组开始,过滤它们并以过滤后的对象列表结束?
当您使用过滤器 .[]
时,您实际上是将数组结果分解为每个项目的单独结果。您最终看到的是一个接一个的结果。
为确保将它们保存为数组,您可以通过用方括号括起过滤器将结果放回数组中:
[
.[] |
select(.Selected != null) |
{
"Head Of House Name",
"Head Of House Phone",
"Head Of House Email",
"Family Phone",
"Family Email"
}
]
或者使用 map()
过滤器将其保留为数组,然后您可以删除 .[]
部分:
map(
select(.Selected != null) |
{
"Head Of House Name",
"Head Of House Phone",
"Head Of House Email",
"Family Phone",
"Family Email"
}
)
如果你问我,你应该坚持map()
。