2014-10-17 50 views
-1

我经历的“漂亮打印JSON” How can I pretty-print JSON in (unix) shell script? 和其他#1的帖子了,但他们只是简单的输入 想好,漂亮的打印复杂的JSON输入

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool 

当我尝试做像这样的东西

echo '{"group" : {list : [1,2,3]}, "list" : ["a","b","c"]}' | python -m json.tool 

它失败。

给我

Expecting property name enclosed in double quotes: line 1 column 13 (char 12) 

PS错误:为什么我试图通过一个复杂的JSON输入?我试图从here


编辑解决问题1:感谢您的及时答复。但是,如果我在寻找像这样输出

{

“组”:{

“清单”:[1,2,3]

},

“清单”: [“a”,“b”,“c”]

}

+1

您可能希望让该网站知道他们的示例无效。 – 2014-10-17 07:56:45

+0

会这样做。谢谢。 – 2014-10-17 08:00:10

回答

1

您的JSON输入无效;你需要引用第一list键:

echo '{"group" : {"list" : [1,2,3]}, "list" : ["a","b","c"]}' | python -m json.tool 
#     ^^^^^^ 

该工具可以处理JSON的任何复杂,只要你给它有效 JSON输入。随着更正错误,Python的输出:

$ echo '{"group" : {"list" : [1,2,3]}, "list" : ["a","b","c"]}' | python -m json.tool 
{ 
    "group": { 
     "list": [ 
      1, 
      2, 
      3 
     ] 
    }, 
    "list": [ 
     "a", 
     "b", 
     "c" 
    ] 
} 
+0

谢谢。现在明白了。 – 2014-10-17 07:57:01

1

在这一行:

"group" : {list : [1,2,3]} 

你有无效的JSON。它期望list是一个字符串,而不是。因此错误。更改:

"group" : {"list" : [1,2,3]} 

将解决该问题。

+0

明白了。我的错。 – 2014-10-17 07:53:26