2

How can I read a JSON variable in shell script ( to be noted "JSON variable " and not JSON File)? I have tried something like,

temp={\"name\":\"Sipdy\",\"time\":\"17:09 1985\",\"place\":\"CA\"}
jq '.time' $temp

and also tried

temp={"name":"Sipdy","time":"17:09 1985","place":"CA"}
jq '.time' $temp

but both the above commands expect a JSON file name in place of "$temp".

Alexander
  • 4,420
  • 7
  • 27
  • 42
T.S
  • 33
  • 6

1 Answers1

1

You need to provide the JSON text as jq's standard input:

$ temp='{"name":"Sipdy","time":"17:09 1985","place":"CA"}'
$ echo $temp | jq .time                                   
"17:09 1985"
$ jq .time <<< $temp
"17:09 1985"

(The second form is a here string.)

Shawn
  • 47,241
  • 3
  • 26
  • 60