如何使用jq根据内部数组中的值过滤对象数组? [英] How to filter an array of objects based on values in an inner array with jq?
问题描述
鉴于此输入:
<预><代码>[{Id":cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b",名称":[居高临下的琼斯",love_hoover"]},{Id":186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa",名称":[foo_data"]},{Id":a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19",名称":[jovial_wozniak"]},{Id":76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623",名称":[bar_data"]}]我正在尝试使用 jq 构建一个过滤器,该过滤器返回所有带有 Id 的对象
不包含数据";在内部 Names
数组中,输出以换行符分隔.对于上述数据,我想要的输出是:
cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2ba4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19
我想我有点接近这个:
(. - select(.Names[] contains("data"))) |.[] .ID
但 select
过滤器不正确且无法编译(获取 error: syntax error, unexpected IDENT
).
非常接近!在您的 select
表达式中,您必须在 contains
之前使用管道 (|
).
此过滤器产生预期的输出.
<预><代码>.- map(select(.Names[] | contains ("data"))) |.[] .IDjqCookbook 有一个语法示例.
<块引用>根据键的内容过滤对象
例如,我只想要类型键包含house"的对象.
$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]'$ echo "$json" |jq -c '.[] |select(.genre | contains("house"))'{流派":深宅"}{流派":进步的房子"}
Colin D 问如何保留数组的 JSON 结构,以便最终输出的是单个 JSON 数组而不是 JSON 对象流.
最简单的方法是将整个表达式包装在一个数组构造函数中:
$ echo "$json" |jq -c '[ .[] |选择(.流派|包含(房子"))]'[{"genre":"deep house"},{"genre":"progressive house"}]
您也可以使用地图功能:
$ echo "$json" |jq -c 'map(select(.genre | contains("house")))'[{"genre":"deep house"},{"genre":"progressive house"}]
map 解包输入数组,将过滤器应用于每个元素,并创建一个新数组.换句话说,map(f)
等价于 [.[]|f]
.
Given this input:
[
{
"Id": "cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b",
"Names": [
"condescending_jones",
"loving_hoover"
]
},
{
"Id": "186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa",
"Names": [
"foo_data"
]
},
{
"Id": "a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19",
"Names": [
"jovial_wozniak"
]
},
{
"Id": "76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623",
"Names": [
"bar_data"
]
}
]
I'm trying to construct a filter with jq that returns all objects with Id
s that do not contain "data" in the inner Names
array, with the output being newline-separated. For the above data, the output I'd like is:
cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b
a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19
I think I'm somewhat close with this:
(. - select(.Names[] contains("data"))) | .[] .Id
but the select
filter is not correct and it doesn't compile (get error: syntax error, unexpected IDENT
).
Very close! In your select
expression, you have to use a pipe (|
) before contains
.
This filter produces the expected output.
. - map(select(.Names[] | contains ("data"))) | .[] .Id
The jq Cookbook has an example of the syntax.
Filter objects based on the contents of a key
E.g., I only want objects whose genre key contains "house".
$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]' $ echo "$json" | jq -c '.[] | select(.genre | contains("house"))' {"genre":"deep house"} {"genre":"progressive house"}
Colin D asks how to preserve the JSON structure of the array, so that the final output is a single JSON array rather than a stream of JSON objects.
The simplest way is to wrap the whole expression in an array constructor:
$ echo "$json" | jq -c '[ .[] | select( .genre | contains("house")) ]'
[{"genre":"deep house"},{"genre":"progressive house"}]
You can also use the map function:
$ echo "$json" | jq -c 'map(select(.genre | contains("house")))'
[{"genre":"deep house"},{"genre":"progressive house"}]
map unpacks the input array, applies the filter to every element, and creates a new array. In other words, map(f)
is equivalent to [.[]|f]
.
这篇关于如何使用jq根据内部数组中的值过滤对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!