Reading JSON array into Julia DataFrame-like type -
given json file, json package happily parses it. if dataframe (or other columnar data structure), way it?
currently, example, have:
using json using dataframes json_str = """ [{ "color": "red", "value": "#f00" }, { "color": "green", "value": "#0f0" }, { "color": "blue", "value": "#00f" }, { "color": "cyan", "value": "#0ff" }, { "color": "magenta", "value": "#f0f" }, { "color": "yellow", "value": "#ff0" }, { "color": "black", "value": "#000" } ] """ function jsontodf(a) ka = union([keys(r) r in a]...) df = dataframe(;dict(symbol(k)=>get.(a,k,na) k in ka)...) return df end = json.parser.parse(json_str) jsontodf(a)
which results in:
7×2 dataframes.dataframe │ row │ color │ value │ ├─────┼───────────┼────────┤ │ 1 │ "red" │ "#f00" │ │ 2 │ "green" │ "#0f0" │ │ 3 │ "blue" │ "#00f" │ │ 4 │ "cyan" │ "#0ff" │ │ 5 │ "magenta" │ "#f0f" │ │ 6 │ "yellow" │ "#ff0" │ │ 7 │ "black" │ "#000" │
and handles missing fields nas. cleaner / faster (julia v0.6+) ?
Comments
Post a Comment