r - How to parse JSON with fromJSON on a dataframe column? -
i have following data.frame 1 column called "json" , 2 rows of json data:
df <- data.frame(json = c('{"client":"abc company","totalusd":7110.0000,"durationdays":731,"familysize":4,"assignmenttype":"long term","homelocation":"australia","hostlocation":"united states","servicename":"service abc","homelocationgeolat":-25.274398,"homelocationgeolng":133.775136,"hostlocationgeolat":37.09024,"hostlocationgeolng":-95.712891}', '{"client":"abc company","totalusd":7110.0000,"durationdays":731,"familysize":4,"assignmenttype":"long term","homelocation":"australia","hostlocation":"united states","servicename":"service xyz","homelocationgeolat":-25.274398,"homelocationgeolng":133.775136,"hostlocationgeolat":37.09024,"hostlocationgeolng":-95.712891}'))
i trying parse json data.frame using fromjson rjson package.
i cast column character type , attempt parse:
> df$json <- as.character(df$json) > final <- fromjson(json_str = df$json)
however, seems give me first row of json, whereas expect 2 rows.
how can parse json data.frame df$json?
you want resultant data frame exercise, so:
do.call(rbind.data.frame, lapply(df$json, rjson::fromjson)) ## client totalusd durationdays familysize assignmenttype homelocation hostlocation servicename homelocationgeolat ## 2 abc company 7110 731 4 long term australia united states service abc -25.2744 ## 21 abc company 7110 731 4 long term australia united states service xyz -25.2744 ## homelocationgeolng hostlocationgeolat hostlocationgeolng ## 2 133.7751 37.09024 -95.71289 ## 21 133.7751 37.09024 -95.71289
the exact same results come from:
do.call(rbind.data.frame, lapply(df$json, jsonlite::fromjson)) do.call(rbind.data.frame, lapply(df$json, rjsonio::fromjson))
Comments
Post a Comment