jq select 数组中的值

jq select value from array

我有以下 JSON 文件,其中包含示例值:

{
    "files": [{
        "fileName": "FOO",
        "md5": "blablabla"
    }, {
        "fileName": "BAR",
        "md5": "alaldlafj"
    }]
}

现在我想要的是 return md5 值,例如文件名是 "FOO"。为此,我在 jq 中有以下声明:

cat <file>.json | jq '.[] | select(.fileName=="FOO")' 

然而回复是:jq: error (at <stdin>:11): Cannot index array with string "fileName"

return 键文件名等于某个参数的 md5 值的正确方法是什么?

找到答案:

cat <file>.json | jq -r '.files[] | select(.fileName=="FOO") | .md5'

或:

cat <file>.json | jq -r '.files[] | select(.fileName=="FOO").md5'

回答更通用的如何select数组中的值select所有文件名:

cat results.json | jq '.files[] | .filename'