Search code examples
jsonrfor-looprcurlrjson

JSON parsing in a for loop in R


I'm trying to write a for loop that will take zip codes, make an API call to a database of Congressional information and then parse out only the parties of congressmen representing at zip code.

The issue is that some of the zip codes have more than one congressman and others have none at all, (an error on the part of the database, I think). That means I need to loop through the count returned by the original pull until there are no more representatives.

The issue is that the number of congressmen representing each zip code is different. Thus, I'd like to be able to write new variable names into my dataframe for each new congressman. That is, if there are 2 congressmen, I'd like to write new columns named "party.1" and "party.2", etc.

I have this code so far and I feel that I'm close, but I'm really stuck on what to do next. Thank you all for your help!

EDIT: I found this way to be easier, but I'm still not getting the results I'm looking for

library(rjson)
library(RCurl)

zips <- (c("10001","92037","90801", "94011")

test <- matrix(nrow=4,ncol=7)
temp <- NULL
tst <- NULL

for (i in 1:length(zips)) {
   for (n in length(temp$count)) {
       temp <- (fromJSON(getURL(paste('https://congress.api.sunlightfoundation.com/legislators/locate?zip=', 
                                 zips[i],'&apikey= 'INSERT YOUR API KEY', sep=""), .opts = list(ssl.verifypeer = FALSE))))
    tst <- try(temp$results[[n]]$party, silent=T)
      if(is(tst,"try-error"))
        test[i,n] <- NA
      else
        test[i,n] <- (temp$results[[n]]$party)
  }
}

Solution

  • install.packages("rsunlight")
    library("rsunlight")
    zips <- c("10001","92037","90801", "94011")
    out <- lapply(zips, function(z) cg_legislators(zip = z))
    # results for some only
    sapply(out, "[[", "count")
    # peek at results for one zip code
    head(out[[1]]$results[,1:4])
    
    bioguide_id   birthday chamber                                                    contact_form
    1     S000148 1950-11-23  senate         http://www.schumer.senate.gov/Contact/contact_chuck.cfm
    2     N000002 1947-06-13   house https://jerroldnadler.house.gov/forms/writeyourrep/default.aspx
    3     M000087 1946-02-19   house                   https://maloney.house.gov/contact-me/email-me
    4     G000555 1966-12-09  senate                       http://www.gillibrand.senate.gov/contact/
    

    You can change as needed within a lapply or for loop to add columns, etc.

    To pull out party could be as simple as lapply(zips, function(z) cg_legislators(zip = z)$results$party).