quinnj / JSON3.jl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to write pretty data?

francispoulin opened this issue · comments

I am trying to translate python code into julia and am finding your library very helpful. Thank you!

One issue I'm having is that I have three dictionaries that I want to write to one json file. I have combined them into one dictionary and the JSON.pretty looks just right, see below. In python when I write it to a file it looks exactly like this. Is it possible to do this in Julia to make it easier to read?

{
    "grid": {
        "Ly": 100000.0,
        "Lz": 3000.0,
        "Ny": 200,
        "lat": 0.09817477042468103,
        "method": "cheb"
    },
    "jet": {
        "L": 10000.0,
        "Umax": 14.6,
        "profile": "Bickley"
    },
    "physics": {
        "N": 0.001,
        "Omega": 7.27220521664304e-05,
        "fy": 0.00014474375121658055,
        "fz": 1.425601518450127e-05,
        "g": 9.81,
        "nu": 0.26
    }
}

Have you looked at the examples in the docs? You should be able to do something like

hello_world = Dict("a" => Dict("b" => 1, "c" => 2), "b" => Dict("c" =>3, "d" => 4))

open("my_new_file.json", "w") do io
    JSON3.pretty(io, hello_world)
end

Thank you @mcmcgrath13 for the quick reply and you are of course correct. That does work beautifully.

To answer your question, yes I did look through the examples and sa both pretty and open but I hadn't seen them used together. I probably should have thought of trying that myself.

Thanks again for the help!