Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

r

jsonlite

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!

like image 823
emehex Avatar asked Dec 25 '22 04:12

emehex


1 Answers

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.

like image 129
alistaire Avatar answered Dec 27 '22 10:12

alistaire