jq select value from array

Marvin Effing picture Marvin Effing · Jun 1, 2016 · Viewed 23.4k times · Source

I have the following JSON file with example values:

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

Now what I want is to return the md5 value where for example the fileName is "FOO". For this I have the following statement in jq:

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

However response back is: jq: error (at <stdin>:11): Cannot index array with string "fileName"

What is the correct way to return the md5 value where the key fileName equals a certain argument?

Answer

Marvin Effing picture Marvin Effing · Jun 1, 2016

Found the answer:

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