简体   繁体   English

在 Shell 脚本中读取 JSON 变量

[英]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)?如何在 shell 脚本中读取 JSON 变量(注意“JSON 变量”而不是 JSON 文件)? 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".但是上面的两个命令都需要一个 JSON 文件名来代替“$temp”。

You need to provide the JSON text as jq's standard input:您需要提供 JSON 文本作为 jq 的标准输入:

$ 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 .) (第二种形式是here string 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM