问题
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".
回答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.)
来源:https://stackoverflow.com/questions/53291536/read-json-variable-in-shell-script