Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the jsonlite package in R?
easy
A. To manage databases
B. To create graphical plots
C. To perform statistical tests
D. To convert R objects to JSON format and back

Solution

  1. Step 1: Understand jsonlite's functionality

    The jsonlite package is designed to handle JSON data in R, allowing conversion between R objects and JSON text.
  2. Step 2: Identify the correct purpose

    Among the options, only converting R objects to JSON and back matches jsonlite's main use.
  3. Final Answer:

    To convert R objects to JSON format and back -> Option D
  4. Quick Check:

    jsonlite = convert R and JSON [OK]
Hint: jsonlite = R to JSON and back conversion [OK]
Common Mistakes:
  • Confusing jsonlite with plotting packages
  • Thinking jsonlite manages databases
  • Assuming jsonlite performs statistical tests
2. Which of the following is the correct syntax to convert an R list my_list to JSON text using jsonlite?
easy
A. jsonlite::toJSON(my_list)
B. jsonlite::fromJSON(my_list)
C. toJSON(my_list, jsonlite)
D. fromJSON(my_list, jsonlite)

Solution

  1. Step 1: Identify the function to convert R to JSON

    The function to convert R objects to JSON in jsonlite is toJSON().
  2. Step 2: Check correct usage with namespace

    Using jsonlite::toJSON(my_list) correctly calls the function from the package.
  3. Final Answer:

    jsonlite::toJSON(my_list) -> Option A
  4. Quick Check:

    toJSON converts R to JSON [OK]
Hint: Use toJSON() to convert R data to JSON [OK]
Common Mistakes:
  • Using fromJSON() to convert R to JSON
  • Passing package name as argument
  • Mixing function names and arguments
3. What will be the output of the following R code?
library(jsonlite)
my_list <- list(name = "Anna", age = 25)
json_text <- toJSON(my_list, auto_unbox = TRUE)
cat(json_text)
medium
A. {"name":"Anna","age":25}
B. ["Anna",25]
C. {"name":["Anna"],"age":[25]}
D. Error: object 'toJSON' not found

Solution

  1. Step 1: Understand toJSON with auto_unbox

    The toJSON() function converts the list to JSON. The auto_unbox=TRUE option removes array brackets for single values.
  2. Step 2: Analyze the output format

    The list has named elements "name" and "age" with single values, so JSON will be an object with those keys and unboxed values.
  3. Final Answer:

    {"name":"Anna","age":25} -> Option A
  4. Quick Check:

    toJSON with auto_unbox = unboxed JSON object [OK]
Hint: auto_unbox=TRUE removes brackets for single values [OK]
Common Mistakes:
  • Ignoring auto_unbox and expecting arrays
  • Confusing JSON arrays with objects
  • Forgetting to load jsonlite package
4. Identify the error in this R code snippet using jsonlite:
library(jsonlite)
json_text <- '{"x":10, "y":20}'
my_data <- toJSON(json_text)
medium
A. Variable my_data is not assigned correctly
B. Missing library(jsonlite) call
C. toJSON() is used instead of fromJSON() to parse JSON text
D. JSON text is not properly formatted

Solution

  1. Step 1: Check function usage for JSON parsing

    The code tries to convert JSON text to R data. The correct function for this is fromJSON(), not toJSON().
  2. Step 2: Confirm other parts are correct

    The JSON text is valid, and the library is loaded, so the main error is the wrong function call.
  3. Final Answer:

    toJSON() is used instead of fromJSON() to parse JSON text -> Option C
  4. Quick Check:

    Use fromJSON() to read JSON text [OK]
Hint: Use fromJSON() to read JSON text, not toJSON() [OK]
Common Mistakes:
  • Using toJSON() to read JSON text
  • Not loading jsonlite package
  • Incorrect JSON string format
5. You have a JSON string representing a list of users:
json_users <- '[{"name":"John","age":30},{"name":"Jane","age":25}]'

How can you convert this JSON into an R data frame with columns name and age using jsonlite?
hard
A. df <- toJSON(json_users)
B. df <- fromJSON(json_users)
C. df <- as.data.frame(toJSON(json_users))
D. df <- as.list(fromJSON(json_users))

Solution

  1. Step 1: Identify function to parse JSON to R

    The fromJSON() function converts JSON text into R objects, including data frames when JSON is an array of objects.
  2. Step 2: Confirm output type matches requirement

    Since the JSON is a list of user objects, fromJSON() returns a data frame with columns for each key.
  3. Final Answer:

    df <- fromJSON(json_users) -> Option B
  4. Quick Check:

    fromJSON() parses JSON array to data frame [OK]
Hint: Use fromJSON() to convert JSON array to data frame [OK]
Common Mistakes:
  • Using toJSON() instead of fromJSON()
  • Converting JSON to list instead of data frame
  • Trying to convert JSON string directly with as.data.frame()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard