Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON with R

Tags:

json

r

I am fairly new to R, but the more use it, the more I see how powerful it really is over SAS or SPSS. Just one of the major benefits, as I see them, is the ability to get and analyze data from the web. I imagine this is possible (and maybe even straightforward), but I am looking to parse JSON data that is publicly available on the web. I am not a programmer by any stretch, so any help and instruction you can provide will be greatly appreciated. Even if you point me to a basic working example, I probably can work through it.

like image 218
Btibert3 Avatar asked Jan 14 '10 03:01

Btibert3


People also ask

Can R read JSON files using?

R can read JSON files using the rjson package.

Which function is used to read a JSON file in R?

Reading the JSON file in R is a very easy and effective process. R provide from JSON() function to extract data from a JSON file. This function, by default, extracts the data in the form of a list. This function takes the JSON file and returns the records which are contained in it.

What does \r mean in JSON?

You're using both "\r" and "/r" in your question, "\r" has special meaning (escape for a return character) which one do you really mean? It appears you're trying to remove the "\r" strings from the JSON output but it's not clear from your question. – ellipse-of-uncertainty.


2 Answers

RJSONIO from Omegahat is another package which provides facilities for reading and writing data in JSON format.

rjson does not use S4/S3 methods and so is not readily extensible, but still useful. Unfortunately, it does not used vectorized operations and so is too slow for non-trivial data. Similarly, for reading JSON data into R, it is somewhat slow and so does not scale to large data, should this be an issue.

Update (new Package 2013-12-03):

jsonlite: This package is a fork of the RJSONIO package. It builds on the parser from RJSONIO but implements a different mapping between R objects and JSON strings. The C code in this package is mostly from the RJSONIO Package, the R code has been rewritten from scratch. In addition to drop-in replacements for fromJSON and toJSON, the package has functions to serialize objects. Furthermore, the package contains a lot of unit tests to make sure that all edge cases are encoded and decoded consistently for use with dynamic data in systems and applications.

like image 122
rcs Avatar answered Sep 18 '22 08:09

rcs


The jsonlite package is easy to use and tries to convert json into data frames.

Example:

library(jsonlite)  # url with some information about project in Andalussia url <- 'https://api.stackexchange.com/2.2/badges?order=desc&sort=rank&site=stackoverflow'  # read url and convert to data.frame document <- fromJSON(txt=url) 
like image 37
joscani Avatar answered Sep 20 '22 08:09

joscani