T.S
T.S

Reputation: 33

Read JSON variable in Shell Script

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".

Upvotes: 2

Views: 247

Answers (1)

Shawn
Shawn

Reputation: 52334

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.)

Upvotes: 1

Related Questions