extract nested list from all 'elements/levels'

I've seen there are already plenty of questions regarding purrr, json and nested lists. They were all very helpful, but I am stuck with one issue which seemed pretty straightforward to me initally.

I have a list, called list_ref (originating from an API request) which has many elements and nested sublists.
I am able to extract the sublist I am interested in for 1 specific list, e.g. with

d <- list_ref[[1]][["Data"]][["Metadaten"]][["Judikatur"]]

What I need is to get the sublist 'Judikatur' for all elements. Not only the first one.
I am missing something obvious. Many thanks.

<!-- language-all: lang-r -->

    #> Warning: package 'tidyr' was built under R version 3.6.3
    #> Warning: package 'forcats' was built under R version 3.6.3
    #> Warning: package 'jsonlite' was built under R version 3.6.2
    #> Attaching package: 'jsonlite'
    #> The following object is masked from 'package:purrr':
    #>     flatten

    api_base_link <- "https://data.bka.gv.at/ris/api/v2.5/judikatur"

    get_res <- GET(url=api_base_link, query=list(Suchworte="Hunger", #search terms
                                                      Norm="AsylG 2005",
                                                      DokumenteProSeite="OneHundred", #results per page
                                                      Seitennumer=1))  #page number; here only first page

    content_res <- content(get_res, as="text")
    content_res_json <- fromJSON(content_res, simplifyDataFrame = F)

    list_ref <- content_res_json$OgdSearchResult$OgdDocumentResults$OgdDocumentReference

    d <- list_ref[[1]][["Data"]][["Metadaten"]][["Judikatur"]]

    list_ref %>% 
      purrr::flatten() %>% 
      map(., magrittr::extract, "Data", "metadaten", "Judikatur")
    #> Error in .x[...]: incorrect number of dimensions

<sup>Created on 2020-04-10 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

Hi zoowalk,

I think you want

map(list_ref , ~.[["Data"]][["Metadaten"]][["Judikatur"]])

Which gives you a list of lists.

How I came up with that was that I looked at your definition of d and said, 'instead of [[1]] I want something which will go through each element of list_ref'. When you use map, the dot in the second argument means 'for each element of list_ref', so I replaced [[1]] with . and mapped over list_ref.

Excellent talk on this: https://www.youtube.com/watch?v=b0ozKTUho0A

Hope that's helpful,

1 Like

Excellent. Many thanks! I'll def will watch the recommended video.
Btw, I when trying out your code, I realized that it is equivalent to
map(list_ref, pluck, "Data", "Metadaten", "Judikatur")

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.