JSON write and read back Arrays

I would like to write some multi-D Array into JSON and parse it back.
I tried using JSON.jl :

julia> using JSON

julia> ar=rand(2,2,2)
2×2×2 Array{Float64,3}:
[:, :, 1] =
0.238296 0.813746
0.156503 0.953343

[:, :, 2] =
0.518232 0.185695
0.644104 0.820459

julia> ar_json=JSON.json(ar)
“[[[0.23829646658262904,0.15650270743089711],[0.8137457610137939,0.9533428752270887]],[[0.5182318303002098,0.6441044583930384],[0.18569459908985997,0.8204588699553217]]]”

julia> ar_decode=JSON.parse(ar_json)
2-element Array{Any,1}:
Any[Any[0.23829646658262904, 0.15650270743089711], Any[0.8137457610137939, 0.9533428752270887]]
Any[Any[0.5182318303002098, 0.6441044583930384], Any[0.18569459908985997, 0.8204588699553217]]

As you can see decoded variable ar_decode is not of the some data type as my source variable ar.
What am I missing?

JSON does not have matrices, only vectors, so your data was represented as vectors of vectors. Also, you may want to change the element type when read. For this particular library, see

but note that other JSON libraries in Julia may have different ways of approaching this problem, so you could consider exploring those too.

After some additional discovering. Using Unmarshaling.jl to map decoded JSON to predefined Julia structure does the trick.

1 Like