You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
autodetect json fields and use jsonlite to parse them into lists #1240
I've encountered data dumps from postgresql with JSON columns in them. It'd be really cool if fread could auto-detect this and use jsonlite to parse those columns into lists.
Right now I'm doing dt[,col := lapply(col, jsonlite::fromJSON)], but it'd be cool if I could skip this step.
Since json objects are pretty analogous to R lists, and data.tables support list columns, I think this would be a really nice feature to have. Even better, when one day you add a fwrite (#580) function to data.table, it can automatically serialize list columns to json strings.
@zachmayer , recently I investigated fread internals - see this PR. So from my experience, it will be hard to detect json and parse it, because all parsing is in plain C. And it will be hard to write json parser in C which maps json objects into R lists. @arunsrinivasan, please, correct me if i'm wrong.
Seems related to fread(..., sep2 ). If it's just a case of a starting [, ending ] and optionally some quoting inside the sep2 separated vector, then shouldn't be too bad. I don't know JSON at all.
Anyway, wanted to get the 'sep2' string into this one, for search later.
@mattdowle To really do it right, you'd want to use jsonlite::fromJSON, which is already really fast and handles all the edge cases correctly. sep2 would get us part of the way there, though, and would be immensely useful.
That said, dt_new[, other := sapply(other, jsonlite::fromJSON)] seems a little... inefficient to me =D. We already parse 64 bit integers using the bit64 package, so maybe this could be another optional dependency for certain datatypes?
changed the title [-][fread] autodetect json fields and use jsonlite to parse them into lists[/-][+]autodetect json fields and use jsonlite to parse them into lists[/+]on May 22, 2019
@zachmayer I doubt we use bit64 for parsing int64, AFAIU we just return object of such class, but parsing is made by Matt in C. Core fread operates in plain C, so no way to call R API of jsonlite package.
I would say wontfix -- JSON is a completely different data structure, don't think it makes sense to for data.table to reinvent the wheel there (user contributions welcome of course).
For example this simdjson parser came up recently & IIUC is blazing fast -- and of course Dirk already built an R wrapper for it 😄 IMO it makes more sense to add contributions to that package to potentially improve the R API of leveraging simdjson, than to spend time teaching fread to parse arbitrarily-nested data structures.
Agree, what is also an important factor, jsonlite is very lightweight package, same like data.table, so adding it as an extra dependency in users code is not that big problem.
Activity
zachmayer commentedon Sep 16, 2015
The use case looks something like this:
Where dt_json looks like this:
(other is a character vector)
And dt_list looks like this:
(other is a list of character vectors)
The idea would be
freadwould generatedt_listrather thandt_json, e.g.:Right now I have to manually run
fromJSON:Since json objects are pretty analogous to R lists, and data.tables support list columns, I think this would be a really nice feature to have. Even better, when one day you add a
fwrite(#580) function to data.table, it can automatically serialize list columns to json strings.dselivanov commentedon Sep 16, 2015
@zachmayer , recently I investigated
freadinternals - see this PR. So from my experience, it will be hard to detect json and parse it, because all parsing is in plainC. And it will be hard to write json parser inCwhich maps json objects into R lists.@arunsrinivasan, please, correct me if i'm wrong.
mattdowle commentedon Apr 7, 2016
Seems related to fread(..., sep2 ). If it's just a case of a starting
[, ending]and optionally some quoting inside the sep2 separated vector, then shouldn't be too bad. I don't know JSON at all.Anyway, wanted to get the 'sep2' string into this one, for search later.
zachmayer commentedon Apr 7, 2016
@mattdowle To really do it right, you'd want to use
jsonlite::fromJSON, which is already really fast and handles all the edge cases correctly.sep2would get us part of the way there, though, and would be immensely useful.That said,
dt_new[, other := sapply(other, jsonlite::fromJSON)]seems a little... inefficient to me =D. We already parse 64 bit integers using the bit64 package, so maybe this could be another optional dependency for certain datatypes?[-][fread] autodetect json fields and use jsonlite to parse them into lists[/-][+]autodetect json fields and use jsonlite to parse them into lists[/+]jangorecki commentedon May 22, 2019
@zachmayer I doubt we use bit64 for parsing int64, AFAIU we just return object of such class, but parsing is made by Matt in C. Core fread operates in plain C, so no way to call R API of
jsonlitepackage.zachmayer commentedon May 22, 2019
There are JSON parsers in C:
https://github.com/DaveGamble/cJSON
https://zserge.com/jsmn.html
https://stackoverflow.com/questions/6673936/parsing-json-using-c
Maybe that's too heavy a dependency to add, but it certainly would be useful to have json support in fread and fwrite.
MichaelChirico commentedon Aug 19, 2019
It should be possible to
includeheaders fromjsonlite:https://github.com/jeroen/jsonlite/blob/master/src/parse.c
Though I'm not sure what that would mean for
pydatatableMichaelChirico commentedon May 31, 2020
I would say
wontfix-- JSON is a completely different data structure, don't think it makes sense to fordata.tableto reinvent the wheel there (user contributions welcome of course).For example this
simdjsonparser came up recently & IIUC is blazing fast -- and of course Dirk already built an R wrapper for it 😄 IMO it makes more sense to add contributions to that package to potentially improve the R API of leveragingsimdjson, than to spend time teachingfreadto parse arbitrarily-nested data structures.jangorecki commentedon May 31, 2020
Agree, what is also an important factor,
jsonliteis very lightweight package, same like data.table, so adding it as an extra dependency in users code is not that big problem.