-1

I have gone through How can I pretty-print JSON in (unix) shell script? and other Stackoverflow posts on "pretty print JSON" but they are only good for simple inputs like,

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

When I try to do something like this

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

it fails.

Gives me the error

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

PS: Why am I trying to pass a complex json input? I'm trying to solve question 1 from here


Edit: Thanks for prompt reply. But what if I'm looking for an output like this

{

"group" : {

"list" : [1,2,3]

},

"list" : ["a","b","c"]

}

Community
  • 1
  • 1
Saurabh Rana
  • 3,350
  • 2
  • 19
  • 22

2 Answers2

1

Your JSON input is invalid; you need to quote the first list key:

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

The tool can handle any complexity of JSON, provided you give it valid JSON input. With the error corrected, Python outputs:

$ echo '{"group" : {"list" : [1,2,3]}, "list" : ["a","b","c"]}' | python -m json.tool
{
    "group": {
        "list": [
            1,
            2,
            3
        ]
    },
    "list": [
        "a",
        "b",
        "c"
    ]
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

In this line:

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

you have invalid json. it is expecting list to be a string, which is not. Hence the error. Changing:

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

will solve the issue.

avi
  • 9,292
  • 11
  • 47
  • 84