Read JSON variable in Shell Script

六眼飞鱼酱① 提交于 2021-02-10 06:34:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!