23.13 JSON
- Examples of toy JSONs from Exercise 23.5.4 Item 1: “Rectangle the
df_col
anddf_row
below. They represent the two ways of encoding a data frame in JSON.”
json_col <- parse_json('
{
"x": ["a", "x", "z"],
"y": [10, null, 3]
}
')
json_row <- parse_json('
[
{"x": "a", "y": 10},
{"x": "x", "y": null},
{"x": "z", "y": 3}
]
')
df_col <- tibble(json = list(json_col))
df_row <- tibble(json = json_row)
df_col |>
unnest_wider(json) |>
unnest_longer(x) |>
unnest_longer(y)
## # A tibble: 9 × 2
## x y
## <chr> <int>
## 1 a 10
## 2 a NA
## 3 a 3
## 4 x 10
## 5 x NA
## 6 x 3
## 7 z 10
## 8 z NA
## 9 z 3
## # A tibble: 3 × 2
## x y
## <chr> <int>
## 1 a 10
## 2 x NA
## 3 z 3