Skip to Content
Course content

171: Working with JSON Data in R

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of a JSON file like a set of nested Russian Matryoshka dolls. On the surface, you have one big doll (the root object). You open it up, and inside you find a few smaller dolls (key-value pairs). Some of those smaller dolls, when opened, reveal even more dolls inside them (nested lists or objects). It's a hierarchy. The trick with JSON in R is that R prefers its data in flat, rectangular spreadsheets—data frames—while JSON is more like a branching tree.

When we map this to R, the "big doll" becomes a named list. The "labels" on the dolls become the names of the list elements, and the "items inside" become the values. If you have a list of similar items (like a list of employees in a company), R can often "flatten" those into a data frame for you, provided the structure is consistent. I've spent a lot of time cleaning up API responses, and I can tell you that the biggest headache isn't the syntax—it's understanding exactly how deep those dolls are nested before you try to pull data out.

Bringing the Outside World In with jsonlite

In the R ecosystem, jsonlite is the heavy lifter. You might see other packages, but this is the one I use in production. The core function you'll live in is fromJSON(). It handles the translation from that JSON string into something R can actually manipulate.

Let's look at a real scenario. Imagine you're pulling data from a movie database API. The response doesn't just give you a table; it gives you a structured object:

library(jsonlite)

# Imagine this string came from an API call via httr or curl
movie_json <- '{
  "title": "Interstellar",
  "release_year": 2014,
  "director": "Christopher Nolan",
  "cast": ["Matthew McConaughey", "Anne Hathaway", "Jessica Chastain"],
  "stats": {
    "runtime": 169,
    "rating": 8.6
  }
}'

# Parse the JSON string into an R object
movie_data <- fromJSON(movie_json)

# Accessing the data
print(movie_data$title)        # "Interstellar"
print(movie_data$cast[1])      # "Matthew McConaughey"
print(movie_data$stats$rating) # 8.6

Notice how jsonlite automatically decided that cast should be a character vector and stats should be another named list. It does a great job of guessing the types, but you always want to check the structure using str(movie_data) after a parse. Trust me, I've wasted hours trying to treat a list as a vector because I didn't check the structure first.

Flattening the Nest

The real power of jsonlite comes when you have a JSON array—basically a list of objects. If every object in that array has the same fields, fromJSON() will automatically simplify it into a data frame. This is the "magic" that makes R and JSON actually get along.

# A JSON array of movies
movies_json <- '[
  {"title": "The Matrix", "year": 1999, "genre": "Sci-Fi"},
  {"title": "Inception", "year": 2010, "genre": "Sci-Fi"},
  {"title": "The Godfather", "year": 1972, "genre": "Crime"}
]'

# This automatically becomes a data frame
movies_df <- fromJSON(movies_json)

# Now you can use standard R data frame operations
sci_fi_movies <- movies_df[movies_df$genre == "Sci-Fi", ]

If the JSON is deeply nested and you don't want to keep typing $stats$details$value, you can use the flatten = TRUE argument within fromJSON(). This attempts to collapse nested objects into a single level by creating names like stats.rating. It's a huge time-saver when dealing with complex API responses.

Sending Data Back Out

Eventually, you'll need to send your results back to a web server or save them for a JavaScript frontend. That's where toJSON() comes in. The most important thing here is the pretty = TRUE argument. Without it, R spits out a single, massive line of text that is impossible for a human to read. If you're debugging, always set pretty = TRUE.

# Let's take that data frame and turn it back into JSON
json_output <- toJSON(movies_df, pretty = TRUE)
cat(json_output)

One quick tip: if you're working with dates or special R objects, toJSON() might behave unexpectedly. Always verify the output of your JSON string before sending it to a production API, or you'll find yourself in a very long debugging session with a frontend developer who is wondering why your dates look like numbers.




📋 Practical Task

Parsing a Complex Weather API Response

You have received the following JSON string from a weather service. The data is nested: there is a top-level object containing a list of "forecasts," and each forecast contains a "temperature" object with "min" and "max" values.

weather_json <- '{
  "city": "Seattle",
  "country": "USA",
  "forecasts": [
    {"date": "2023-10-01", "temp": {"min": 10, "max": 15}, "condition": "Rain"},
    {"date": "2023-10-02", "temp": {"min": 11, "max": 17}, "condition": "Cloudy"},
    {"date": "2023-10-03", "temp": {"min": 9, "max": 14}, "condition": "Rain"}
  ]
}'

Your goal:

  1. Use jsonlite::fromJSON() to parse this string. Use the flatten = TRUE argument to make the temperature data easier to access.
  2. Extract the forecasts portion into its own data frame.
  3. Calculate the average max temperature across all days in the forecast.
  4. Print the final average to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.