0
0
R Programmingprogramming~20 mins

JSON with jsonlite in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON with jsonlite Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this JSON conversion?
Consider this R code using jsonlite:
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)
A["Alice",30,[90,85,88]]
B{"name":"Alice","age":30,"scores":[90,85,88]}
C{"name":["Alice"],"age":[30],"scores":[90,85,88]}
D{"name":"Alice","age":"30","scores":[90,85,88]}
Attempts:
2 left
💡 Hint
Remember that auto_unbox = TRUE removes array brackets for single values.
Predict Output
intermediate
2:00remaining
What does this JSON parsing produce?
Given this R code:
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)
A
List of 3
 $ fruit : chr "apple"
 $ count : int 5
 $ colors: chr [1:2] "red" "green"
B
List of 3
 $ fruit : chr [1:1] "apple"
 $ count : chr "5"
 $ colors: list of 2
C
List of 3
 $ fruit : chr "apple"
 $ count : chr "5"
 $ colors: chr [1:2] "red" "green"
D
List of 3
 $ fruit : chr "apple"
 $ count : int 5
 $ colors: list of 1
Attempts:
2 left
💡 Hint
Check how fromJSON converts JSON arrays and numbers.
🔧 Debug
advanced
2:00remaining
Why does this JSON conversion fail?
This R code throws an 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)
AThe list is missing names for elements, causing a syntax error.
BtoJSON requires numeric vectors only, so the integer 1 causes an error.
Cjsonlite cannot convert functions to JSON, causing an error.
DThe function argument x is undefined, causing a runtime error.
Attempts:
2 left
💡 Hint
Think about what data types JSON supports.
📝 Syntax
advanced
2:00remaining
Which option correctly parses nested JSON?
Given this JSON string:
{"person":{"name":"Bob","age":25},"hobbies":["reading","swimming"]}

Which R code correctly parses it into a list with nested elements?
AfromJSON('{"person":{"name":"Bob","age":25},"hobbies":reading,swimming}')
BfromJSON('{person:{name:"Bob",age:25},hobbies:["reading","swimming"]}')
CfromJSON('{"person":{"name":"Bob","age":25},"hobbies":[reading,swimming]}')
DfromJSON('{"person":{"name":"Bob","age":25},"hobbies":["reading","swimming"]}')
Attempts:
2 left
💡 Hint
Check JSON syntax rules for strings and keys.
🚀 Application
expert
2:00remaining
How many elements are in the parsed JSON array?
This R code parses a JSON array:
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)
A2
B3
C6
D1
Attempts:
2 left
💡 Hint
Check the structure of parsed data and what length() returns for data frames.