Read a JSON file dumped by python

Hi,

I save a pyhton dictionary in a JSON file (.json) and now i would like to import this dictionary in julia. Which is the best option?

I save my dictionary in pyhton as follow,

a_file = open('path/dict_name.json', 'w')
json.dump(my_dict, a_file)
a_file.close()

Thanks.

if it’s a valid json file, use JSON3.jl or JSON.jl package

3 Likes

Thanks @jling, Could you help me with the import code?

JSON3.read(read("path/dict_name.json", String))
1 Like

It didn’t work for me, I installed the “JSON” package and then imported my file like this:

dict_name = JSON.read(read("path/dict_name.json", String))

but, throw this error:

SystemError: opening file "": No such file or directory

Then, i tried installing “JSON3” package, but threw errors:

> Pkg.add("JSON3")
Resolving package versions...
No Changes to `C:\Users\semed\.julia\environments\v1.5\Project.toml`
No Changes to `C:\Users\semed\.julia\environments\v1.5\Manifest.toml`

JSON3 and JSON are two different packages. I don’t see error, just no changes.

I know, but when i run, throw this:

> using JSON3

 Info: Precompiling JSON3 [0f8b85d8-7281-11e9-16c2-39a750bddbf1]
└ @ Base loading.jl:1278
WARNING: could not import StructTypes.OrderedStruct into JSON3
ERROR: LoadError: LoadError: UndefVarError: OrderedStruct not defined
Stacktrace:
 [1] top-level scope at C:\Users\semed\.julia\packages\JSON3\IvnMR\src\structs.jl:591
 [2] include(::Function, ::Module, ::String) at .\Base.jl:380
 [3] include at .\Base.jl:368 [inlined]
 [4] include(::String) at C:\Users\semed\.julia\packages\JSON3\IvnMR\src\JSON3.jl:1
 [5] top-level scope at C:\Users\semed\.julia\packages\JSON3\IvnMR\src\JSON3.jl:163
 [6] include(::Function, ::Module, ::String) at .\Base.jl:380
 [7] include(::Module, ::String) at .\Base.jl:368
 [8] top-level scope at none:2
 [9] eval at .\boot.jl:331 [inlined]
 [10] eval(::Expr) at .\client.jl:467
 [11] top-level scope at .\none:3
in expression starting at C:\Users\semed\.julia\packages\JSON3\IvnMR\src\structs.jl:591
in expression starting at C:\Users\semed\.julia\packages\JSON3\IvnMR\src\JSON3.jl:163

Failed to precompile JSON3 [0f8b85d8-7281-11e9-16c2-39a750bddbf1] to C:\Users\semed\.julia\compiled\v1.5\JSON3\1p699_kXwKq.ji.

And, if i ried with “JSON” package (that I could install and use), throw this error:

SystemError: opening file "": No such file or directory

And, i have the file in this directory.

what version of JSON3 is this?

JSON -> JSON v0.21.1
JSON3 -> JSON3 v1.7.2

the precompile leading to error looks weird, maybe your environment is messed up somehow.

seems related