0

Consider this example from the tidyjson GH page:

#devtools::install_github("sailthru/tidyjson")
library(tidyjson)
library(dplyr)

json <- '[{"name": "bob", "age": 32}, {"name": "susan", "age": 54}]'

json %>%
  as.tbl_json %>%
  gather_array %>%
  spread_values(
    name = jstring("name"),
    age = jnumber("age")
  )

# A tibble: 2 x 4
  document.id array.index name    age
*       <int>       <int> <chr> <dbl>
1           1           1 bob      32
2           1           2 susan    54

I have extracted all JSON keys and put it into a vector. I am looking for an efficient solution in the vein of (just jstring is fine) to create a tabular dataset:

features <- c("name", "age", "gender", "education", "income", ...)

# spread_values(features = jstring(features))

How do I generalise the list at the spread_values step; i.e. in the above example, how can I condense name and age to a one-liner? The name of the columns are always the same as the JSON keys. I have tried apply family (lapply/sapply) functions and a for-loop, but always get the same assignment error:

Error: unexpected '=' in...

Cheers.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I appreciate the edit; however I am actually interested in learning why this assignment error happens and what is the right way of creating such loops in R. Although it is informative (and useful) to know how to generalise this particular function, I feel like the general application would be of higher value. –  Aug 15 '19 at 21:20

1 Answers1

1

See this question

There are many answers there but the easiest is probably using the jsonlite package:

library(tidyjson)
library(dplyr)

json <- '[{"name": "bob", "age": 32}, {"name": "susan", "age": 54}]'

json %>%
  fromJSON

   name age
1   bob  32
2 susan  54
Eli Berkow
  • 2,628
  • 1
  • 12
  • 22