A seemingly simple JSON file that I would like to parse with R:
tmp_extract <- "{"encrypted_values":[{"name_a":"value_a"}, {"name_b":"value_b"}, {"name_c":"value_c"}]}"
An attempt with jsonlite::fromJSON
produces a dataframe with as many columns as there are names and only a value per column.
tmp_extract |>
jsonlite::fromJSON()
$encrypted_values
name_a name_b name_c
1 value_a <NA> <NA>
2 <NA> value_b <NA>
3 <NA> <NA> value_c
(I also tried tmp_extract |> tidyjson::spread_all()
but without success.)
The desired output:
# A tibble: 3 × 2
name value
<chr> <chr>
1 name_a value_a
2 name_b value_b
3 name_c value_c
2
Answers
Try:
Using the CRAN package rjsoncons and JMESPath query syntax, create in JSON an object with fields name and value
then ‘pivot’ to a tibble (or data.frame)
This approach is performant and flexible; it can be applied to NDJSON too.