Importing data from a JSON file into R [duplicate] Importing data from a JSON file into R [duplicate] json json

Importing data from a JSON file into R [duplicate]


First install the rjson package:

install.packages("rjson")

Then:

library("rjson")json_file <- "http://api.worldbank.org/country?per_page=10&region=OED&lendingtype=LNX&format=json"json_data <- fromJSON(paste(readLines(json_file), collapse=""))

Update: since version 0.2.1

json_data <- fromJSON(file=json_file)


jsonlite will import the JSON into a data frame. It can optionally flatten nested objects. Nested arrays will be data frames.

> library(jsonlite)> winners <- fromJSON("winners.json", flatten=TRUE)> colnames(winners)[1] "winner" "votes" "startPrice" "lastVote.timestamp" "lastVote.user.name" "lastVote.user.user_id"> winners[,c("winner","startPrice","lastVote.user.name")]    winner startPrice lastVote.user.name1 68694999          0              Lamur> winners[,c("votes")][[1]]                            ts user.name user.user_id1 Thu Mar 25 03:13:01 UTC 2010     Lamur     686949992 Thu Mar 25 03:13:08 UTC 2010     Lamur     68694999


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 <- do.call(rbind, m)

gives information on the votes in your example.