juliajulia-dataframe

How to read record format json in Julia?


I am able to read a json file and convert into dataframe using below code.

df = open(jsontable, "normal.json") |> DataFrame

normal.json looks like below,

{"col1":["thasin", "hello", "world"],"col2":[1,2,3],"col3":["abc", "def", "ghi"]}

So final df has,

3×3 DataFrame
│ Row │ col1   │ col2  │ col3   │
│     │ String │ Int64 │ String │
├─────┼────────┼───────┼────────┤
│ 1   │ thasin │ 1     │ abc    │
│ 2   │ hello  │ 2     │ def    │
│ 3   │ world  │ 3     │ ghi    │

But, the same code is not working for record formatted json file.

the format is list like {column -> value}, … , {column -> value}

My sample json

{"billing_account_id":"0139A","credits":[],"invoice":{"month":"202003"},"cost_type":"regular"}
{"billing_account_id":"0139A","credits":[1.45],"invoice":{"month":"202003"},"cost_type":"regular"}
{"billing_account_id":"0139A","credits":[2.00, 3.56],"invoice":{"month":"202003"},"cost_type":"regular"}

Expected output:

  billing_account_id cost_type      credits              invoice
0             0139A   regular           []  {'month': '202003'}
1             0139A   regular       [1.45]  {'month': '202003'}
2             0139A   regular  [2.0, 3.56]  {'month': '202003'}

This can be done in python like below,

data = []
for line in open("sample.json", 'r'):
    data.append(json.loads(line))
print(data)
df=pd.DataFrame(data)

How to do this in Julia?


Solution

  • Note that your file is not a valid JSON (its lines are valid JSON, not the whole file).

    You can do this like this:

    julia> using DataFrames, JSON3
    
    julia> df = JSON3.read.(eachline("sample.json")) |> DataFrame;
    
    julia> df.credits = Vector{Float64}.(df.credits);
    
    julia> df.invoice = Dict{Symbol,String}.(df.invoice);
    
    julia> df
    3×4 DataFrame
    │ Row │ billing_account_id │ credits                    │ invoice                │ cost_type │
    │     │ String             │ Array{Float64,1}           │ Dict{Symbol,String}    │ String    │
    ├─────┼────────────────────┼────────────────────────────┼────────────────────────┼───────────┤
    │ 1   │ 0139A              │ 0-element Array{Float64,1} │ Dict(:month=>"202003") │ regular   │
    │ 2   │ 0139A              │ [1.45]                     │ Dict(:month=>"202003") │ regular   │
    │ 3   │ 0139A              │ [2.0, 3.56]                │ Dict(:month=>"202003") │ regular   │
    

    The transformations on :credits and :invoice columns are to make them of type that is easy to work with (otherwise they use types that are defined internally by JSON3.jl).

    A more advanced option is to do it in one shot by specifying the row schema using a NamedTuple type e.g.:

    julia> df = JSON3.read.(eachline("sample.json"),
                            NamedTuple{(:billing_account_id, :credits, :invoice, :cost_type),Tuple{String,Vector{Float64},Dict{String,String},String}}) |>
                DataFrame
    3×4 DataFrame
    │ Row │ billing_account_id │ credits                    │ invoice                 │ cost_type │
    │     │ String             │ Array{Float64,1}           │ Dict{String,String}     │ String    │
    ├─────┼────────────────────┼────────────────────────────┼─────────────────────────┼───────────┤
    │ 1   │ 0139A              │ 0-element Array{Float64,1} │ Dict("month"=>"202003") │ regular   │
    │ 2   │ 0139A              │ [1.45]                     │ Dict("month"=>"202003") │ regular   │
    │ 3   │ 0139A              │ [2.0, 3.56]                │ Dict("month"=>"202003") │ regular   │