Instead of using base R for importing JSON files, it is best to use a package called rjson
. This will import the JSON file or text as a list, including nested elements. For example, using rjson
and tidyverse
to obtain the before and after answers in a tibble. Unfortunately, as answers before and after are different lengths, this complicates things and I had to create the tibbles independently then bind them. There will be NA values.
# Install and load the package required to read JSON files.
# install.packages("rjson")
library(rjson)
# Install and load tidyverse
# install.packages(tidyverse)
library(tidyverse)
# Read the JSON text
text='{"content":"VIDEO","progress":30,"userData":{"id":"BE7C","age":"54","sex":"m"},"answersBefore":{"0_0":1,"0_1":2,"0_2":2,"0_3":1,"0_4":1,"0_5":1,"1_1":2,"1_2":6,"1_0":3,"1_3":1,"1_4":4,"1_5":5,"2_0":2,"2_1":2,"3_0":4,"3_1":6,"3_2":6,"4_0":2,"5_0":7,"5_1":5,"5_2":5,"5_3":7,"5_4":6,"6_0":2,"7_0":7},"answersAfterwards":{"0_0":2,"0_1":1,"0_2":2,"0_3":1,"0_4":2,"0_5":1,"1_0":2,"1_1":1,"1_2":4,"1_3":2,"1_4":3,"1_5":3,"2_0":2,"2_1":2,"3_0":2,"3_1":4,"3_2":4,"4_0":2,"5_0":7,"5_1":5,"5_2":5,"5_3":7,"5_4":6,"6_0":2,"8_0":2,"8_1":2,"8_2":5,"9_0":7},"timeStamps":{"start":"2021-09-08T09:11:03.610Z","firstQuestionnaireFinished":"2021-09-08T09:30:13.814Z","contentFinished":"2021-09-08T09:36:13.377Z","secondQuestionnaireFinished":"2021-09-08T09:38:24.230Z"},"points":0}'
json=fromJSON(text)# Can also use the file parameter to read from a file `file="test.json"`
# Access some of the data
json$userData$age
#> [1] "54"
json$content
#> [1] "VIDEO"
# Create the answers before data and convrt it into a tibble
before=as_tibble(
json$answersBefore
)%>%
# add the time colum with the value before
mutate(time="berfore")
before
#> # A tibble: 1 x 26
#> `0_0` `0_1` `0_2` `0_3` `0_4` `0_5` `1_1` `1_2` `1_0` `1_3` `1_4` `1_5` `2_0`
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 2 1 1 1 2 6 3 1 4 5 2
#> # ... with 13 more variables: `2_1` <dbl>, `3_0` <dbl>, `3_1` <dbl>,
#> # `3_2` <dbl>, `4_0` <dbl>, `5_0` <dbl>, `5_1` <dbl>, `5_2` <dbl>,
#> # `5_3` <dbl>, `5_4` <dbl>, `6_0` <dbl>, `7_0` <dbl>, time <chr>
# Create the answers after data and convrt it into a tibble
after=as_tibble(
json$answersAfterwards
)%>%
# add the time colum with the value after
mutate(time="after")
after
#> # A tibble: 1 x 29
#> `0_0` `0_1` `0_2` `0_3` `0_4` `0_5` `1_0` `1_1` `1_2` `1_3` `1_4` `1_5` `2_0`
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2 1 2 1 2 1 2 1 4 2 3 3 2
#> # ... with 16 more variables: `2_1` <dbl>, `3_0` <dbl>, `3_1` <dbl>,
#> # `3_2` <dbl>, `4_0` <dbl>, `5_0` <dbl>, `5_1` <dbl>, `5_2` <dbl>,
#> # `5_3` <dbl>, `5_4` <dbl>, `6_0` <dbl>, `8_0` <dbl>, `8_1` <dbl>,
#> # `8_2` <dbl>, `9_0` <dbl>, time <chr>
# Bind the rows into one tibble
bind_rows(
before, after
)
#> # A tibble: 2 x 30
#> `0_0` `0_1` `0_2` `0_3` `0_4` `0_5` `1_1` `1_2` `1_0` `1_3` `1_4` `1_5` `2_0`
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 2 1 1 1 2 6 3 1 4 5 2
#> 2 2 1 2 1 2 1 1 4 2 2 3 3 2
#> # ... with 17 more variables: `2_1` <dbl>, `3_0` <dbl>, `3_1` <dbl>,
#> # `3_2` <dbl>, `4_0` <dbl>, `5_0` <dbl>, `5_1` <dbl>, `5_2` <dbl>,
#> # `5_3` <dbl>, `5_4` <dbl>, `6_0` <dbl>, `7_0` <dbl>, time <chr>,
#> # `8_0` <dbl>, `8_1` <dbl>, `8_2` <dbl>, `9_0` <dbl>
Created on 2021-10-01 by the reprex package (v0.3.0)