Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing data from a JSON file into R [duplicate]

Tags:

json

r

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.

like image 1000
user313967 Avatar asked Apr 11 '10 15:04

user313967


People also ask

How do I read a JSON file into a DataFrame?

To read the files, we use read_json() function and through it, we pass the path to the JSON file we want to read. Once we do that, it returns a “DataFrame”( A table of rows and columns) that stores data.

What does R JSON () do?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. This package converts JSON objects into R objects and vice-versa.

What is Jsonlite?

jsonlite: A Simple and Robust JSON Parser and Generator for R. A reasonably fast JSON parser and generator, optimized for statistical data and the web. Offers simple, flexible tools for working with JSON in R, and is particularly powerful for building pipelines and interacting with a web API.


1 Answers

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) 
like image 99
rcs Avatar answered Oct 05 '22 23:10

rcs