Maiasaura
Maiasaura

Reputation: 32986

How do I parse the results from a JSON output into a data frame?

In trying to analyze tweets from a hashtag, I am able to obtain 100 results at a time via this call.

curl -i "http://otter.topsy.com/search.json?q=esa2012&window=a&perpage=100&offset=0" >> esa2012_0.json

resulting in a file like this (JSON file in link).

How do I read this into R and convert it to a data.frame?

Thus far I am able to skip the first several (non-JSON) lines like so:

library(XML)
library(RJSONIO)
file0 <- scan(file = "~/Desktop/data/esa2012_0.json", skip = 18, what= "raw")

but the resulting read becomes difficult to coerce.

Upvotes: 1

Views: 413

Answers (1)

Ari B. Friedman
Ari B. Friedman

Reputation: 72769

fromJSON(file0) should do it.

I typically run it with ,simplify=FALSE and construct the data.frame myself rather than trying to have it simplify for me.

Note you don't need the command line call to curl if you use the RCurl library:

library(RJSONIO)
library(RCurl)
j <- getURL("http://blah?mine=yours")
fromJSON(j)

Upvotes: 3

Related Questions