简体   繁体   English

给定带有转义双引号的字符串,如何使用 jq 美化 json

[英]How to prettify json with jq given a string with escaped double quotes

I would like to pretty print a json string I copied from an API call which contains escaped double quotes.我想漂亮地打印一个我从包含转义双引号的 API 调用中复制的 json 字符串。

Similar to this:与此类似:

"{\"name\":\"Hans\", \"Hobbies\":[\"Car\",\"Swimming\"]}"

However when I execute pbpaste | jq "."但是,当我执行pbpaste | jq "." pbpaste | jq "." the result is not changed and is still in one line.结果没有改变,仍然在一行中。

I guess the problem are the escaped double quotes, but I don't know how to tell jq to remove them if possible, or any other workaround.我想问题是转义的双引号,但我不知道如何告诉jq如果可能的话删除它们,或者任何其他解决方法。

What you have is a JSON string which happens to contain a JSON object.你所拥有的是一个 JSON 字符串,它恰好包含一个 JSON 对象。 You can decode the contents of the string with the fromjson function.您可以使用fromjson函数解码字符串的内容。

$ pbpaste | jq "."
"{\"name\":\"Hans\", \"Hobbies\":[\"Car\",\"Swimming\"]}"
$ pbpaste | jq "fromjson"
{
  "name": "Hans",
  "Hobbies": [
    "Car",
    "Swimming"
  ]
}

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

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