简体   繁体   English

使用 jq 解析 JSON 错误

[英]Parsing errors of JSON using jq

I have a curl command which results in the following example json:我有一个 curl 命令,它会生成以下示例 json:

json={"id":"12345","key":"ABC-DEF","url":"https://google.com"}

Now, I want to parse this, and get the key out of it and store it in a variable.现在,我想解析它,并从中取出密钥并将其存储在一个变量中。 What I did was the following:我所做的是以下内容:

json={"id":"12345","key":"ABC-DEF","url":"https://google.com"}
ID=$(echo $json | jq '.key' )

But the above gives me a error as: parse error: Invalid numeric literal at line 1, column 4 .但上面给了我一个错误: parse error: Invalid numeric literal at line 1, column 4 Can someone help me with this?有人可以帮我弄这个吗? Thanks!谢谢!

You will need to quote the input string so the shell doesn't do anything with your string您将需要引用输入字符串,以便 shell 不对您的字符串执行任何操作

json='{"id":"12345","key":"ABC-DEF","url":"https://google.com"}'
ID=$(echo "$json" | jq '.key' )

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

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