json 如何使用jq根据内部数组中的值过滤对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26701538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter an array of objects based on values in an inner array with jq?
提问by Abe Voelker
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 jqthat returns all objects with Ids that do notcontain "data" in the inner Namesarray, with the output being newline-separated. For the above data, the output I'd like is
我正在尝试使用jq构造一个过滤器,该过滤器返回内部数组中不包含“数据”的所有带有Ids 的对象,输出以换行符分隔。对于上述数据,我想要的输出是Names
cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b
a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19
I think I'm somewhat close with this:
我想我有点接近这个:
(. - select(.Names[] contains("data"))) | .[] .Id
but the selectfilter is not correct and it doesn't compile (get error: syntax error, unexpected IDENT).
但select过滤器不正确,它不能编译(获取error: syntax error, unexpected IDENT)。
回答by Iain Samuel McLean Elder
Very close! In your selectexpression, you have to use a pipe (|) before contains.
很接近!在您的select表达式中,您必须|在contains.之前使用管道 ( ) 。
This filter produces the expected output.
此过滤器产生预期的输出。
. - map(select(.Names[] | contains ("data"))) | .[] .Id
The jq Cookbookhas an example of the syntax.
的JQ食谱具有语法的一个例子。
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"}
根据键的内容过滤对象
例如,我只想要流派键包含“房子”的对象。
$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]' $ echo "$json" | jq -c '.[] | select(.genre | contains("house"))' {"genre":"deep house"} {"genre":"progressive house"}
Colin Dasks 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.
Colin D询问如何保留数组的 JSON 结构,以便最终输出的是单个 JSON 数组而不是 JSON 对象流。
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].
map 解包输入数组,将过滤器应用于每个元素,并创建一个新数组。换句话说,map(f)相当于[.[]|f]。
回答by jq170727
Here is another solution which uses any/2
这是另一个使用any/2 的解决方案
map(select(any(.Names[]; contains("data"))|not)|.Id)[]
with the sample data and the -roption it produces
使用示例数据及其-r生成的选项
cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b
a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19

