How to parse JSON in a DataFrame column using R -


how come here ...

| id | json request                                                          | ============================================================================== |  1 | {"user":"xyz1","weightmap": {"p1":0,"p2":100}, "domains":["a1","b1"]} | ------------------------------------------------------------------------------ |  2 | {"user":"xyz2","weightmap": {"p1":100,"p2":0}, "domains":["a2","b2"]} | ------------------------------------------------------------------------------ 

to here (the requirement make table of json in column 2):

| user | p1 | p2 | domains |  ============================ | xyz1 |  0 |100 | a1, b1  | ---------------------------- | xyz2 |100 | 0  | a2, b2  | ---------------------------- 

here code generate data.frame:

raw_df <-    data.frame(     id   = 1:2,     json =        c(         '{"user": "xyz2", "weightmap": {"p1":100,"p2":0}, "domains": ["a2","b2"]}',          '{"user": "xyz1", "weightmap": {"p1":0,"p2":100}, "domains": ["a1","b1"]}'       ),      stringsasfactors = false   ) 

could not flatten parameter work expected needed unlist , "re-list" before rbinding do.call:

library(jsonlite)  do.call( rbind,            lapply(raw_df$json,                    function(j) as.list(unlist(fromjson(j, flatten=true)))         )       )      user   weightmap.p1 weightmap.p2 domains1 domains2 [1,] "xyz2" "100"        "0"          "a2"     "b2"     [2,] "xyz1" "0"          "100"        "a1"     "b1"     

admittedly, require further processing since coerces lines character.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -