How to read JSON in R where values is a list? Need help parsing nested data! or Struggling to read JS

16 Replies, 1790 Views

"Struggling to read JSON in R where values is a list—any tips for handling nested structures?"

Hey folks!

So I'm trying to read json in r where values is a list, but man, this nested stuff is killing me. 😅

I got the file loaded with `jsonlite::fromJSON()`, but when I try to access the deeper layers, it's either NULL or some weird nested mess.

Like, why can't R just give me the dang values without all the extra steps?

Anyone got a quick fix or a favorite package for this? Maybe `purrr` or something?

Appreciate any help—my sanity's on the line here!

(Also, if you've got a sec, how do you flatten these things properly? Keep seeing mixed advice.)

Thanks! 🚀
Hey! I feel your pain—nested JSON in R can be a headache. If you're using `jsonlite::fromJSON()`, try adding `flatten = TRUE` to simplify the structure.

For deeper nesting, `purrr::map()` is a lifesaver. Like, `map(data, "nested_field")` can dig into layers without losing your mind.

Also, check out the `tidyjson` package—it’s built for this exact problem (read json in r where values is a list).

Good luck!
Ugh, nested JSON is the worst. Here's a quick trick: after loading with `jsonlite`, use `str()` to see the structure. Helps you spot where things are hiding.

If you need to flatten, `rlist::list.flatten()` can sometimes work better than `jsonlite`'s built-in flattening.

And yeah, `purrr` is gold for this stuff. `map_depth()` lets you target specific levels.

Hope that saves your sanity!
Been there! The `tidyjson` package is a game-changer for read json in r where values is a list. It lets you use tidyverse-style piping to navigate nested structures.

Example:
```
library(tidyjson)
your_json %>% enter_object("nested_field") %>% gather_array()
```

Makes it way more intuitive than base R or `jsonlite` alone.
If you're stuck with `jsonlite`, try `simplifyDataFrame = FALSE` in `fromJSON()`. Sometimes the auto-conversion to data frames messes things up.

For flattening, `jsonlite::flatten()` works, but it’s not perfect. You might need to manually unlist() or `purrr::pluck()` to get what you need.

Also, this guide saved me: [https://jennybc.github.io/purrr-tutorial...tutorial/).
Try `jsonlite::fromJSON()` with `simplifyVector = FALSE`. Sometimes the default settings overcomplicate things.

For flattening, `data.table::rbindlist()` can help if the nested bits are similar.

Also, debugging with `View()` in RStudio helps visualize the mess.

---

Wow, thanks everyone! Didn’t expect so many tips. Tried `tidyjson` and `purrr::map()`—way better than my brute-force approach.

Still hitting a snag with super-deep nesting though. Anyone know how to handle lists inside lists inside lists? Like, `data[[1]][[2]][[3]]` feels... wrong.

Also, that `pandas` suggestion is tempting, but gotta stick with R for now.

Thanks again! 🚀
Nested JSON? Big oof. Here’s my lazy fix:

1. Use `jsonlite::fromJSON()` with `flatten = TRUE`.
2. If it’s still messy, `dplyr::as_tibble()` can sometimes tidy it up.

Not elegant, but gets the job done.

Also, `purrr::list_flatten()` is underrated for this.
Pro tip: `rjson` is another option for read json in r where values is a list. It’s less aggressive with flattening than `jsonlite`, so you might have more control.

For nested stuff, I write tiny helper functions with `[[` to drill down. Like:
```
get_nested <- function(x, path) {
Reduce(`[[`, path, init = x)
}
```

Works like a charm!
Honestly, I gave up and just use Python for nested JSON. `pandas.json_normalize()` is *chef’s kiss*.

But if you’re R-only, `purrr` + `tidyr::unnest()` is your best bet.

Here’s a tutorial I used: [https://r4ds.had.co.nz/iteration.html](h...tion.html).



Users browsing this thread: 1 Guest(s)