json - jsonlite's fromJSON is returning a list of 2 lists instead of a df -


i'm following fcc's documentation download metadata proceedings.

i don't believe can post data can free api key.

my code results in listed list of 2 lists instead of structured df json format.

my goal have dataframe each json element it's own column.. normal df.

library(httr) library(jsonlite)  datahere = "c:/fcc/" setwd(datahere)  url <- "https://publicapi.fcc.gov/ecfs/filings?api_key=<key here>&proceedings.name=14-28&sort=date_disseminated,desc" datadf <- get(url) datajson <- content(datadf, as="text") datajson <- fromjson(datajson)  # nas datajson2 <- lapply(datajson, function(x) {   x[sapply(x, is.null)] <- na   unlist(x) })  x <- do.call("rbind", datajson2) x <- as.data.frame(x) 

the json nested, need put little more thought converting between list , data.frame. logic below pulls out data.frame of 25 filings (102 variables) , 10 aggregations (25 variables).

# tackle filings object filings_df <- ldply(datajson$filings, function(x) {   # removes null list elements   x[sapply(x, is.null)] <- na   # converts named character vector   unlisted_x <- unlist(x)   # converts named character vector data.frame   # 1 column , rows each element   d <- as.data.frame(unlisted_x)   # need transpose data.frame because    # rows should columns, , don't check names when converting   d <- as.data.frame(t(d), check.names=f)   # assign actual names based on original    # unlisted character vector   colnames(d) <- names(unlisted_x)   # return ldply function, automatically stack them   return(d) })  # tackle aggregations object # same exact logic create data.frame aggregations_df <- ldply(datajson$aggregations, function(x) {   # removes null list elements   x[sapply(x, is.null)] <- na   # converts named character vector   unlisted_x <- unlist(x)   # converts named character vector data.frame   # 1 column , rows each element   d <- as.data.frame(unlisted_x)   # need transpose data.frame because    # rows should columns, , don't check names when converting   d <- as.data.frame(t(d), check.names=f)   # assign actual names based on original    # unlisted character vector   colnames(d) <- names(unlisted_x)   # return ldply function, automatically stack them   return(d) }) 

Comments