1

I have a JSON file from which I am trying to get a value using below

[a223180n@363748ln41dk012 ~]$ cat status.json| jq  '.details.server1.status'
"UP"

In my script, I have the option where the user will enter the server name. So I need the script to pick that value and update the jq command accordingly I am using below and it is failing.

jq is 1.6 version

[a223180n@363748ln41dk012 ~]$ cat status.json| jq --arg server server1'.details.$server.status'
jq: error: syntax error, unexpected '$', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.details.$service.status
jq: 1 compile error
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
Ready2work
  • 35
  • 6

1 Answers1

2
< status.json jq --arg server server1 '.details[$server].status'

The dot operator is a special version of the [] operator that only works with literal keys. You have a variable for a key here, so you need the [] operator.

hobbs
  • 223,387
  • 19
  • 210
  • 288