Hi,

I need to serialize a DataFrame to JSON. I have read this:

   - https://github.com/JuliaStats/DataFrames.jl/issues/184
   
but my case is a little different.

If my DataFrame looks like this:

julia> df
8x2 DataFrames.DataFrame
| Row | A | B   |
|-----|---|-----|
| 1   | 1 | "M" |
| 2   | 2 | "F" |
| 3   | 3 | "F" |
| 4   | 4 | "M" |
| 5   | 5 | "F" |
| 6   | 6 | "M" |
| 7   | 7 | "M" |
| 8   | 8 | "F" |


Then I need my JSON to look like this (in order to talk to another API):

[

{"A": 1, "B": "M"},

{"A": 2, "B": "F"}, 

{"A": 3, "B": "F"},

{"A": 4, "B": "M"},

{"A": 5, "B": "F"},

{"A": 6, "B": "M"},

{"A": 7, "B": "M"},

{"A": 8, "B": "F"},

]


In Matlab, I would create an array of "structs". In Julia, I'm thinking I 
would need to dynamically create a type

immutable row
    A::Int
    B::AbstractString
end

 
based on the names in the DataFrame, which should be generic and not 
confined to two. Then I would construct an array of "row"s and finally 
using JSON.jl, serialize via JSON.json.

Is there a better/easier way to do this?

Thank you.

Reply via email to