4.8 (Reponse) Formats: JSON
- Response often in JSON or XML format (cf. Munzert et al. 2014, Chapter 3)
http_type(#)
: Check format
- JSON (Javascript Object Notation,
*.json
)- Structure: Data stored in key-value pairs. Why? Lightweight, more flexible than traditional table format.
- Various data types possible (strings, numbers etc.)
- Curly brackets embrace objets; square brackets enclose arrays (vectors)
- objects (
{"name": "peter","phone":"397483"}
) - arrays (
[1910, 1911]
)
- objects (
jsonlite
package: UsefromJSON
function to read JSON data into R- But many packages have their own specific functions to read data in JSON format
- Syntax example
- Structure: Data stored in key-value pairs. Why? Lightweight, more flexible than traditional table format.
- R functions to extract/format content
#
= API responsewriteLines(content(#, type = "text"))
: Inspect & printout contentcontent(#, as = "parsed")
: Parse contentlibrary(jsonlite); fromJSON(content(#, type = "text")); fromJSON(content(#, type = "text"), simplifyDataFrame = TRUE)
: Parse with jsonlite
References
Munzert, Simon, Christian Rubba, Peter Meißner, and Dominic Nyhuis. 2014. Automated Data Collection with R: A Practical Guide to Web Scraping and Text Mining. John Wiley & Sons.