json 如何使用jq检查数组中是否存在元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43259563/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:49:54  来源:igfitidea点击:

How to check if element exists in array with jq

arraysjsonbashmembershipjq

提问by idmitriev

I have an array and I need to check if elements exists in that array or to get that element from the array using jq, fruit.json:

我有一个数组,我需要检查该数组中是否存在元素或使用 jq, fruit.json从数组中获取该元素:

{
    "fruit": [
        "apple", 
        "orange",
        "pomegranate",
        "apricot",
        "mango"
    ]
}


cat fruit.json | jq '.fruit .apple' 

does not work

不起作用

回答by peak

The semantics of 'contains' is not straightforward at all. In general, it would be better to use 'index' to test if an array has a specific value, e.g.

“包含”的语义一点也不简单。一般来说,最好使用 'index' 来测试数组是否具有特定值,例如

.fruit | index( "orange" )

IN/1

输入/1

If your jq has IN/1then a better solution is to use it:

如果您的 jq 有IN/1那么更好的解决方案是使用它:

.fruit as $f | "orange" | IN($f[])

If your jq has first/1(as does jq 1.5), then here is a fast definition of IN/1to use:

如果您的 jq 具有first/1(与 jq 1.5 一样),那么这里是使用的快速定义IN/1

def IN(s): first((s == .) // empty) // false;

回答by tefozi

[WARNING: SEE THE COMMENTS AND ALTERNATIVE ANSWERS.]

[警告:请参阅评论和替代答案。]

cat fruit.json | jq '.fruit | contains(["orange"])'

回答by aaa

For future visitors, if you happen to have the array in a variable and want to check the inputagainst it, and you have jq 1.5 (without IN), your best option is indexbut with a second variable:

对于未来的访问者,如果您碰巧将数组放在一个变量中并想根据它检查输入,并且您有 jq 1.5(没有 IN),那么您最好的选择是index使用第二个变量:

.inputField as $inputValue | $storedArray|index($inputValue)

.inputField as $inputValue | $storedArray|index($inputValue)

This is functionally equivalent to .inputField | IN($storedArray[]).

这在功能上等同于.inputField | IN($storedArray[]).

回答by markusk

To have jqreturn success if the array fruitcontains "apple", and error otherwise:

jq如果数组fruit包含则返回成功"apple",否则返回错误:

jq -e '.fruit[]|select(. == "apple")' fruit.json >/dev/null

To output the element(s) found, omit >/dev/null. If searching for a fixed string, this isn't very relevant, but it might be if the selectexpression might match different values, e.g. if it's a regexp.

要输出找到的元素,请省略>/dev/null。如果搜索固定字符串,这不是很相关,但可能是select表达式可能匹配不同的值,例如,如果它是正则表达式。

To output only distinct values, pass the results to unique.

要仅输出不同的值,请将结果传递给unique

jq '[.fruit[]|select(match("^app"))]|unique' fruit.json

will search for all fruits starting with app, and output unique values. (Note that the original expression had to be wrapped in []in order to be passed to unique.)

将搜索所有以 开头的水果app,并输出唯一值。(请注意,必须将原始表达式包裹起来[]才能传递给unique。)

回答by Reino

If you're open to using something other than jq, then I can highly recommend Xidel.
With it you can combine JSONiq and XPath/XQuery to process JSON!

如果您愿意使用 以外的其他东西jq,那么我强烈推荐Xidel
有了它,您可以结合 JSONiq 和 XPath/XQuery 来处理 JSON!

To have it simply return a boolean:

让它简单地返回一个布尔值:

$ xidel -s fruit.json -e '$json/contains((fruit)(),"apple")'
true

To have it return the element if the array fruitcontains "apple":

如果数组fruit包含“apple”,则让它返回元素:

$ xidel -s fruit.json -e '$json/(fruit)()[contains(.,"apple")]'
apple

Above is "XPath notation". "Dot notation" (like jq):

上面是“XPath 符号”。“点符号”(如jq):

$ xidel -s fruit.json -e '($json).fruit()[contains(.,"apple")]'
apple