Pretty printing JSON in JuliaIO/JSON.jl


#1

Doesn’t pretty printing of JSON strings with JSON.jl work? The README doesn’t explicitly mention pretty printing, but indentation is mentioned all over the code and there are print methods with an indentation parameter.

Here’s the sample JSON string from the README page and my attempt at printing it with 4 spaces of indentation:

julia> s = "{\"a_number\" : 5.0, \"an_array\" : [\"string\", 9]}"
"{\"a_number\" : 5.0, \"an_array\" : [\"string\", 9]}"

julia> JSON.print(s,4)
"{\"a_number\" : 5.0, \"an_array\" : [\"string\", 9]}"

And here’s another sample if someone wants to test something with a bit of nesting.

ss = "{\"breakpoints\":{\"1\":{\"year\":2015,\"value\":33.3,\"unit\":1},\"2\":{\"year\":2040,\"value\":40,\"unit\":1},\"3\":{\"year\":2050,\"value\":40,\"unit\":1},\"4\":{\"year\":2100,\"value\":0,\"unit\":2}},\"parameter\":2.2}"

#2

You need to parse it first

julia> JSON.print(JSON.parse(s), 4)
{
    "an_array": [
        "string",
        9
    ],
    "a_number": 5.0
}
julia> JSON.print(JSON.parse(ss), 4)
{
    "parameter": 2.2,
    "breakpoints": {
        "4": {
            "year": 2100,
            "unit": 2,
            "value": 0
        },
        "1": {
            "year": 2015,
            "unit": 1,
            "value": 33.3
        },
        "2": {
            "year": 2040,
            "unit": 1,
            "value": 40
        },
        "3": {
            "year": 2050,
            "unit": 1,
            "value": 40
        }
    }
}