Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq select value from array

Tags:

json

jq

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?

like image 384
Marvin Effing Avatar asked Jun 01 '16 08:06

Marvin Effing


2 Answers

Found the answer:

cat <file>.json | jq -r '.files[] | select(.fileName=="FOO") | .md5' 
like image 130
Marvin Effing Avatar answered Oct 02 '22 15:10

Marvin Effing


or:

cat <file>.json | jq -r '.files[] | select(.fileName=="FOO").md5' 
like image 33
Alex Berger Avatar answered Oct 02 '22 13:10

Alex Berger