0

I have some json data which looks like below:

{"spark.master": "local"}

I have this stored in a shell variable called json_data

json_data='{"spark.master": "local"}'

and parsing this using the jq shell utility.

As seen above, the key contains a period (.) character. If I try to read it directly using jq, it works fine

$ json_data='{"spark.master": "local"}'
$ val=$(echo $json_data | jq .'"spark.master"')
$ echo $val
"local"

But if this key is stored in a variable, its not working

$ key="spark.master"
$ val=$(echo $json_data | jq .'$key')
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
.$key 
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.$key
jq: 2 compile errors


$ val=$(echo $json_data | jq ."$key")
$ echo $val
null

Can someone please help me identify what is going wrong here?

Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31

0 Answers0