filtering data using parameters

252 views Asked by At

I have this command that is working..

cat  ~/Desktop/results.json |  jq '.[] | .environmentStatuses[].deploymentResult | select(.key.entityKey.key=="39583746-39747586") | .lifeCycleState '

I want to pass the entity key as variable , tried the below ones ,but none seems to work-

enkey="39583746-39747586"

cat  ~/Desktop/results.json |  jq '.[] | .environmentStatuses[].deploymentResult | select(.key.entityKey.key=="""${enkey}""") | .lifeCycleState '

cat  ~/Desktop/results.json |  jq '.[] | .environmentStatuses[].deploymentResult | select(.key.entityKey.key=="${enkey}") | .lifeCycleState '
2

There are 2 answers

5
Jeff Mercado On BEST ANSWER

When trying to use extra parameters in your filters, use the --arg option to pass them in. Don't rely on the shell to insert it into your filter string, keep that separate.

jq --arg key "$enkey" '.[] |
  .environmentStatuses[].deploymentResult |
  select(.key.entityKey.key == $key) |
  .lifeCycleState' ~/Desktop/results.json
1
hoisu On

This worked for me :

  jq '.[] | .environmentStatuses[].deploymentResult |
  select(.key.entityKey.key == "'$key'") |
  .lifeCycleState' ~/Desktop/results.json 

--arg does not works as expected ...