RJ-*_*dam 31 command-line json jq
我有一个json文件,在对象中包含一个json字符串:
{
"requestType": "POST",
"response": {
"size": 78,
"text": "{\"recordID\":123, \"title\":\"Hello World\", \"content\":\"Lorem ipsum...\"}"
}
}
Run Code Online (Sandbox Code Playgroud)
jq.response.text使用json命令行interpereter,jq,我需要将字符串的内容作为json插入.
当我运行此命令时:
jq '.response.text | @json'
Run Code Online (Sandbox Code Playgroud)
输出: "\"{\\\"recordID\\\":123, \\\"title\\\":\\\"Hello World\\\", \\\"content\\\":\\\"Lorem ipsum...\\\"}\""
我得到一些奇怪的转义json字符串而不是json,我可以通过这样的东西访问:.response.text | @json | .recordID.
我意识到该@json函数将采用json并输出一个json转义字符串,因此必须有另一种方式,但@text似乎没有做任何事情.
有没有办法将转义的json字符串转换为实际的json,我可以用这样的命令解析:jq '.response.text | @json | .title'得到这个输出:"Hello World"?
Jef*_*ado 40
使用fromjson.
它将字符串解析为适当的json值. tojson(和@json)反过来采用json值并将其转换为字符串.
所以你可以这样做:
.response.text | fromjson.title
Run Code Online (Sandbox Code Playgroud)
S P*_*owo 12
你也可以这样做:
jq -r '.response.text' | jq '.recordID'
Run Code Online (Sandbox Code Playgroud)