Challenge - 5 Problems
JSON with jsonlite Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JSON conversion?
Consider this R code using jsonlite:
What is the exact output printed by
library(jsonlite) my_list <- list(name = "Alice", age = 30, scores = c(90, 85, 88)) json_text <- toJSON(my_list, auto_unbox = TRUE) cat(json_text)
What is the exact output printed by
cat(json_text)?R Programming
library(jsonlite) my_list <- list(name = "Alice", age = 30, scores = c(90, 85, 88)) json_text <- toJSON(my_list, auto_unbox = TRUE) cat(json_text)
Attempts:
2 left
💡 Hint
Remember that auto_unbox = TRUE removes array brackets for single values.
✗ Incorrect
The toJSON function with auto_unbox = TRUE converts single values like name and age to JSON scalars, not arrays. The scores vector remains an array.
❓ Predict Output
intermediate2:00remaining
What does this JSON parsing produce?
Given this R code:
What is the structure printed by
library(jsonlite)
json_str <- '{"fruit":"apple","count":5,"colors":["red","green"]}'
parsed <- fromJSON(json_str)
str(parsed)What is the structure printed by
str(parsed)?R Programming
library(jsonlite) json_str <- '{"fruit":"apple","count":5,"colors":["red","green"]}' parsed <- fromJSON(json_str) str(parsed)
Attempts:
2 left
💡 Hint
Check how fromJSON converts JSON arrays and numbers.
✗ Incorrect
fromJSON converts JSON strings to character vectors, numbers to integers, and JSON arrays to vectors or lists depending on content. Here colors is a character vector of length 2.
🔧 Debug
advanced2:00remaining
Why does this JSON conversion fail?
This R code throws an error:
What is the cause of the error?
library(jsonlite) my_data <- list(a = 1, b = function(x) x + 1) toJSON(my_data)
What is the cause of the error?
R Programming
library(jsonlite) my_data <- list(a = 1, b = function(x) x + 1) toJSON(my_data)
Attempts:
2 left
💡 Hint
Think about what data types JSON supports.
✗ Incorrect
JSON format cannot represent functions. jsonlite throws an error when trying to convert a function object.
📝 Syntax
advanced2:00remaining
Which option correctly parses nested JSON?
Given this JSON string:
Which R code correctly parses it into a list with nested elements?
{"person":{"name":"Bob","age":25},"hobbies":["reading","swimming"]}Which R code correctly parses it into a list with nested elements?
Attempts:
2 left
💡 Hint
Check JSON syntax rules for strings and keys.
✗ Incorrect
Option D uses valid JSON syntax with double quotes around keys and string values. Others have missing quotes or invalid JSON.
🚀 Application
expert2:00remaining
How many elements are in the parsed JSON array?
This R code parses a JSON array:
What is the value of
library(jsonlite)
json_arr <- '[{"id":1,"val":10},{"id":2,"val":20},{"id":3,"val":30}]'
parsed <- fromJSON(json_arr)
length(parsed)What is the value of
length(parsed)?R Programming
library(jsonlite)
json_arr <- '[{"id":1,"val":10},{"id":2,"val":20},{"id":3,"val":30}]'
parsed <- fromJSON(json_arr)
length(parsed)Attempts:
2 left
💡 Hint
Check the structure of parsed data and what length() returns for data frames.
✗ Incorrect
fromJSON converts the JSON array of objects into a data frame with 3 rows and 2 columns. length() on a data frame returns the number of columns, which is 2.