Search code examples
rjsonjsonliterjsonio

Extracting json values using a key


I have a JSON string called test in which some elements contain more than one key (for example, foo and bar).

My goal is to only extract the values of foo. How can one do this with R?

I have tried converting to both matrix and data.frame but this does not help solve the issue.

> test
[1] "{\"foo\":[1,2,3],\"bar\":[0]}" "{\"foo\":[1]}"                 "{\"foo\":[4], \"bar\":[1]}"   
[4] "{\"foo\":[2]}"                 "{\"foo\":[1,2]}"               "{\"foo\":[3]}" 

Any help would be appreciated

dput(test)
c("{\"foo\":[1,2,3],\"bar\":[0]}", "{\"foo\":[1]}", "{\"foo\":[4], \"bar\":[1]}", 
"{\"foo\":[2]}", "{\"foo\":[1,2]}", "{\"foo\":[3]}")

Solution

  • We can use the fromJSON to convert to a data.frame and then extract the foo column which is a list column

    library(jsonlite)
    lapply(paste0("[", test, "]"), function(x) unlist(fromJSON(x)$foo))
    

    Or paste the elements into a single string and then do the fromJSON

    fromJSON(paste0("[", paste(test, collapse=","), "]"))$foo
    #[[1]]
    #[1] 1 2 3
    
    #[[2]]
    #[1] 1
    
    #[[3]]
    #[1] 4
    
    #[[4]]
    #[1] 2
    
    #[[5]]
    #[1] 1 2
    
    #[[6]]
    #[1] 3
    

    Or using tidyverse

    library(tidyverse)
    str_c(test, collapse=",") %>%
        str_c("[", ., "]") %>%
        fromJSON %>%
        pull(foo)