Extract, format and separate JSON already stored in a data frame column

烈酒焚心 提交于 2019-12-18 13:48:08

问题


How might I parse and process JSON that already lives inside a data frame?

Sample data:

df <- data.frame(
    id = c("x1", "x2"), 
    y = c('[{"Property":"94","Value":"Error"},{"Property":"C1","Value":"Found Match"},{"Property":"C2","Value":"Address Mismatch"}]', '[{"Property":"81","Value":"XYZ"},{"Property":"D1","Value":"Blah Blah"},{"Property":"Z2","Value":"Email Mismatch"}]')
)

I want to extract, format and separate the raw JSON in column y into orderly columns, ideally with library(jsonlite).

Thanks in advance!


回答1:


Using jsonlite and the tidyverse:

library(tidyverse)
library(jsonlite)

df %>% mutate(y = map(y, ~fromJSON(as.character(.x)))) %>% unnest()

# Source: local data frame [6 x 3]
# 
#       id Property            Value
#   <fctr>    <chr>            <chr>
# 1     x1       94            Error
# 2     x1       C1      Found Match
# 3     x1       C2 Address Mismatch
# 4     x2       81              XYZ
# 5     x2       D1        Blah Blah
# 6     x2       Z2   Email Mismatch

or without purrr,

df %>% rowwise() %>% mutate(y = list(fromJSON(as.character(y)))) %>% unnest()

or with just dplyr and jsonlite,

df %>% rowwise() %>% do(data.frame(id = .$id, fromJSON(as.character(.$y))))

or with just base R and jsonlite,

do.call(rbind, 
        Map(function(id, y){data.frame(id, fromJSON(as.character(y)))}, 
            df$id, df$y))

All return the same thing, so pick which makes the most sense to you.



来源:https://stackoverflow.com/questions/37997283/extract-format-and-separate-json-already-stored-in-a-data-frame-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!