I have written a script to retrieve certain value from file.json
. It works if I provide the value to jq select
, but the variable doesn't seem to work (or I don't know how to use it).
#!/bin/sh
#this works ***
projectID=$(cat file.json | jq -r '.resource[] | select(.username=="[email protected]") | .id')
echo "$projectID"
[email protected]
#this does not work *** no value is printed
projectID=$(cat file.json | jq -r '.resource[] | select(.username=="$EMAILID") | .id')
echo "$projectID"
jq -r --arg var "$var" '.[$var]'
stackoverflow.com/questions/34745451/…EMAILID
is between single quotes and does not get expanded. Write it as'"$EMAILID"'
to take it out of the single quotes.