rjsonpurrrrjson

How can I best flatten a nested list to a data.frame in R?


I would like to convert information from a rest api, which comes in form of json, to a data.frame. The list is nested and theoretically I could repeatedly call purrr::flatten() to get to the bottom of the list and then extract the information using for example purrr:::map_dfr and magrittr:::extract. However, this is very domain specific and doesn't work nicely when extracting information from multiple "hierarchies". I have the following set-up in R:

library(rjson)

url <- "https://api3.geo.admin.ch/rest/services/api/SearchServer?searchText=Avenue de Lavaux 63, 1009 Pully&origins=address&type=locations"
result <- rjson::fromJSON(file = URLencode(url))

Two questions arise:

  1. How can I nicely extract attributes like detail, x and y and write them to a data.frame?
  2. On top of that, how can I directly extract values by their names. That is how to extract the values for weight, x, y, and detail.

Thank you very much.


Solution

  • You can unlist the result and extract x and y like this:

    res <- unlist(result)
    res['results.attrs.x']
    # results.attrs.x 
    #  "151398.09375"
    
    res['results.attrs.y']
    # results.attrs.y 
    #  "540429.3125"
    

    You can get the names of all other values like this:

    names(res)
    #[1] "results.id"  "results.weight"  "results.attrs.origin"         
    #    "results.attrs.geom_quadindex" "results.attrs.zoomlevel"     
    #[6] "results.attrs.featureId" "results.attrs.lon" "results.attrs.detail"   
    #    "results.attrs.rank" "results.attrs.geom_st_box2d" "results.attrs.lat"
    #    "results.attrs.num" "results.attrs.y" "results.attrs.x"  "results.attrs.label" 
    

    Then you can combine them in a dataframe:

    res_df <- data.frame(
      X = res['results.attrs.x'],
      Y = res['results.attrs.y']
    )