tags:

views:

211

answers:

2

Is there a way to import data from a JSON file into R? More specifically, the file is an array of JSON objects with string fields, objects, and arrays. The RJSON Package isn't very clear on how to deal with this http://cran.r-project.org/web/packages/rjson/rjson.pdf.

+2  A: 
library("rjson")
json_file <- "http://webonastick.com/uscl/feeds/uscl.json.txt"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
rcs
+1  A: 

An alternative package is RJSONIO. To convert a nested list, lapply can help:

l <- fromJSON('[{"winner":"68694999",  "votes":[ {"ts":"Thu Mar 25 03:13:01 UTC 2010", "user":{"name":"Lamur","user_id":"68694999"}}, {"ts":"Thu Mar 25 03:13:08 UTC 2010", "user":{"name":"Lamur","user_id":"68694999"}}], "lastVote":{"timestamp":1269486788526,"user":{"name":"Lamur","user_id":"68694999"}},"startPrice":0}]')
m <- lapply(l[[1]]$votes, function(x) c(x$user$name, x$user$user_id, x$ts))
m <- matrix(unlist(m), ncol=3, byrow=TRUE)
data.frame(m)

gives information on the votes in your example.

Karsten W.