title: Extracting data from an API using R
body: <p>I have access to some telemetry data in azure (specifically all the API calls for customers using a mobile app). I have used the httr package in R to request the data over a 3 minute period and assess the response like so (obviously have my own app ID and key which I have not included below): </p>
  
  <pre><code>install.packages("httr")
  library(httr)
  
  r1 &lt;- GET("https://api.applicationinsights.io/v1/apps/application-ID/query?timespan=PT0.05H&amp;query=requests", add_headers("X-Api-Key" = "my-unique-key"))
  
  r1
  
  ####### response object ########
  # Response [https://api.applicationinsights.io/v1/apps/application-ID/query?timespan=PT0.05H&amp;query=requests]
  # Date: 2018-01-11 15:55
  # Status: 200
  # Content-Type: application/json; charset=utf-8
  # Size: 84.7 kB
  </code></pre>
  
  <p>In the environment window, I can see r1 is a list of 10 and that there are 84,652 raw values:</p>
  
  <p><a href="https://i.stack.imgur.com/5maEv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5maEv.png" alt="enter image description here"></a></p>
  
  <p>I can also use the content function to see I have a list of 1:</p>
  
  <pre><code>r2 &lt;- content(r1)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/RyTUX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RyTUX.png" alt="enter image description here"></a></p>
  
  <p>I have two questions really: </p>
  
  <p>1) How do I make sense of these outputs in the environment window? What do they tell me about the structure of my data (I think it's JSON based on content type description) </p>
  
  <p>2) Is there a way to retrieve the data and get it into tabular format (a data frame)? I don't understand how to query the data. I read this article, but couldn't apply it to my data: <a href="https://tclavelle.github.io/blog/r_and_apis/" rel="nofollow noreferrer">https://tclavelle.github.io/blog/r_and_apis/</a></p>
  
  <p>Any help would be appreciated.</p>
  
  <hr>
  
  <p><strong>UPDATE 19/01/18</strong></p>
  
  <p>I used jalind's suggestion. See below for code and outputs:</p>
  
  <pre><code>library(httr)
  library(jsonlite)
  
  r1 &lt;- GET("https://api.applicationinsights.io/v1/apps/application-ID/query?timespan=PT0.05H&amp;query=requests", add_headers("X-Api-Key" = "my-unique-key"))
  
  #convert to a character string
  r2 &lt;- rawtoChar(r1$content)  
  
  #check the class is character
  class(r2)    
  
  # now extract JSON from string object
  r3 &lt;- fromJSON(r2)
  
  # convert to a data frame - this returns a data frame with columns called name, columns and rows 
  x &lt;- as.data.frame(r3[[1]])  
  
  # column headings data frame (there are 37 columns - see example of first 3 columns below):               
  c &lt;- as.data.frame(x$columns)
  
  #                       name      type
  #                  timestamp    datetime
  #                         id     string
  #                     source     string
  
  # data frame with 37 columns and all rows of telemetry data (only showing first 4 columns of this data frame):
  
  r &lt;- as.data.frame(x$rows)
  #           X1                               X2                X3                  X4
  # 1   2018-01-19T10:29:25.4Z       |aticCNxxxx=.f83assss_     &lt;NA&gt;          GET /Cards/Cardtype1
  # 2   2018-01-19T10:29:30.226Z     |tX6Xz0xxxxx=.27cxcxae_    &lt;NA&gt;          GET /AddressLookup/Address
  # 3   2018-01-19T10:29:45.327Z     |OgfPbicLues=.f83a9a1f_    &lt;NA&gt;          POST /Account/MobileDevice
  # 4   2018-01-19T10:29:46.078Z     |V5MwpXXxxxxx=.f83axxxx_   &lt;NA&gt;          GET /Cards/Cardtype1
  # 5   2018-01-19T10:30:00.427Z     |Jok8wxxxxxx=.7be33aaa_    &lt;NA&gt;          GET /cards/Cardtype1
  </code></pre>