Getting and cleaning data - JSON Flashcards
JSON SALIENT FACTS
Javascript Object Notation
Lightweight data storage
Common format for data from application programming interfaces (APIs)
Similar structure to XML but different syntax/format
Data stored as
Numbers (double)
Strings (double quoted)
Boolean (true or false)
Array (ordered, comma separated enclosed in square brackets [])
Object (unorderd, comma separated collection of key:value pairs in curley brackets {})
JSON PACKAGE & FIRST STEPS
install.packages(“jsonlite”)
library(jsonlite)
reading data into memory
jsonData <- fromJSON(“https://api.github.com/users/jtleek/repos”)
JSON EXPLORING DATA
Use loading variable as handle to refer to data in memory
Find 1st level headings:
> names(jsonData)
Drill to secon level by referring to a chosen heading using $
> names(jsonData$owner)
List content
> jsonData$owner$login
WRITING DATA FRAME TO JSON
Start with a DF assigned to the variable iris
Transform DF into a JSON format
> myjson <- toJSON(iris, pretty=TRUE)
Read the result
> cat(myjson)
Convert back from JSON
> iris2 <- fromJSON(myjson)
Display result
> head(iris2)
JSON RESOURCES
http://www.json.org/
A good tutorial on jsonlite :
http://www.r-bloggers.com/new-package-jsonlite-a-smarter-json-encoderdecoder/