I want to write .json
from DataFrame
But I want to write some element to be indented, and some are not indented
Here is a minimal example
using DataFrames
using JSON
function JSON.show_json(io::SC, s::CS, df::DataFrame)
JSON.begin_array(io)
for (i, row) in enumerate(eachrow(df))
JSON.indent(io)
JSON.begin_object(io)
for el in row
if isa(el[2], Array{T} where T <: AbstractString) || isa(el[2], Array{T} where T <: Number)
JSON.show_key(io, el[1])
JSON.show_json(io, s, el[2])
else
JSON.show_pair(io, s, el)
end
end
JSON.end_object(io)
i != nrow(df) && JSON.delimit(io)
end
JSON.end_array(io)
end
df = DataFrame(element = [1], array = [[100,200]])
JSON.json(df, 2) |> clipboard
Which gives me
[
{
"element": 1,
"array": [
100,
200
]
}
]
What I want it to be is
[
{
"element": 1,
"array": [100,200]
}
]
I’ve figured that I should switch between PreetyContext
and CompactContext
in IO
based on datatype would be an elegant solution, but I don’t know how to implement it.
Thank for your time