エスケープされた json を jq で整形する


どういうことか

こういうエスケープされた json 部分を整形して表示したい

{
  "a" : 1,
  "b" : 2,
  "c" : "{\"id\":\"hoge\",\"parent\":\"abc\"}\n"
}

解決法

fromjson を使う。

$ pbpaste
{
  "a" : 1,
  "b" : 2,
  "c" : "{\"id\":\"hoge\",\"parent\":\"abc\"}\n"
}
$ pbpaste | jq '.c | fromjson'
{
  "id": "hoge",
  "parent": "abc"
}

参考

Use jq to parse a JSON String - Stack Overflow
https://stackoverflow.com/questions/35154684/use-jq-to-parse-a-json-string

jq Manual (development version)
https://stedolan.github.io/jq/manual/