title: How to download IMF Data Through JSON in R
body: <p>I recently took an interest in retrieving data in R through JSON. Specifically, I want to be able to access data through the IMF. I know virtually nothing about JSON so I will share what I [think I] know so far, and what I have accomplished. </p>
  
  <p>I browsed their web page for JSON, which helped a little bit. It gave me the start point URL. Here is the web page; <code>http://datahelp.imf.org/knowledgebase/articles/667681-using-json-restful-web-service</code></p>
  
  <p>I managed to download (using the <code>GET()</code> and the <code>fromJSON()</code> functions) some lists, which are really bulky. I know enough about the lists that the "call" was successful, but I cannot for the life of me get actual data. So far, I have been trying to use the <code>rawToChar()</code> function on the "content" data but I am virtually stuck there. </p>
  
  <p>If anything, I managed to create data frames that contain the codes, which I presume would be used somewhere in the JSON link. Here is what I have. </p>
  
  <pre><code>      all.imf.data = fromJSON("http://dataservices.imf.org/REST/SDMX_JSON.svc/Dataflow/")
        str(all.imf.data)
  
        #all.imf.data$Structure$Dataflows$Dataflow$Name[[2]] #for the catalogue of sources
        catalogue1 = cbind(all.imf.data$Structure$Dataflows$Dataflow$KeyFamilyRef,
                                      all.imf.data$Structure$Dataflows$Dataflow$Name[[2]])
        catalogue1 = catalogue1[,-2] # catalogue of all the countries
  
  
        data.structure = fromJSON("http://dataservices.imf.org/REST/SDMX_JSON.svc/DataStructure/IFS")
        info1 = data.frame(data.structure$Structure$Concepts$ConceptScheme$Concept[,c(1,4)])
        View(data.structure$Structure$CodeLists$CodeList$Description)
        str(data.structure$Structure$CodeLists$CodeList$Code)
  
                  #Units
                  units = data.structure$Structure$CodeLists$CodeList$Code[[1]]
  
                  #Countries
                  countries = data.frame(data.structure$Structure$CodeLists$CodeList$Code[[3]])
                  countries = countries[,-length(countries)]
  
  
                  #Series Codes
                  codes = data.frame(data.structure$Structure$CodeLists$CodeList$Code[[4]])
                  codes = codes[,-length(codes)]
  
  # all.imf.data   # JSON from the starting point, provided on the website
  # catalogue1     # data frame of all the data bases, International Financial Statistics, Government Financial Statistics, etc.
  # codes          # codes for the specific data sets (GDP, Current Account, etc).
  # countries      # data frame of all the countries and their ISO codes
  # data.structure # large list, with starting URL and endpoint "IFS". Ideally, I want to find some data set somewhere within this data base.  
  "info1"         # looks like parameters for retrieving the data (for instance, dates, units, etc).
  
  # units         # data frame that indicates the options for units 
  </code></pre>
  
  <p>I would just like some advice about how to go about retrieving any data, something as simple as GDP (PPP) for a constant year. I have been following an article in R blogs (which retrieved data in the EU's database) but I cannot replicate the procedure for the IMF. I feel like I am close to retrieving something useful but I cannot quite get there. Given that I have data frames that contain the names for the databases, the series and the codes for the series, I think it is just a matter of figuring out how to construct the appropriate URL for getting the data, but I could be wrong. </p>
  
  <p>Provided in the data frame <code>codes</code> are the codes for the data sets I presume. Is there a way to make a call for the data for, let's say, the US for <code>BK_DB_BP6_USD</code>, which is "Balance of Payments, Capital Account, Total, Debit, etc"? How should I go about doing this in the context of R? </p>