Bird
Raised Fist0
Rest APIprogramming~20 mins

JSON as standard format in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON Mastery Badge
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 parsing code?
Consider this JavaScript code that parses a JSON string and accesses a property.

What will be logged to the console?
Rest API
const jsonString = '{"name": "Alice", "age": 30}';
const obj = JSON.parse(jsonString);
console.log(obj.name);
AAlice
Bundefined
CSyntaxError
Dnull
Attempts:
2 left
💡 Hint
Think about what JSON.parse does to a JSON string.
🧠 Conceptual
intermediate
1:30remaining
Why is JSON preferred as a standard format in REST APIs?
Which of the following is the main reason JSON is widely used as a standard format in REST APIs?
AIt requires special software to read
BIt supports complex binary data natively
CIt is human-readable and easy to parse by machines
DIt is only compatible with JavaScript
Attempts:
2 left
💡 Hint
Think about readability and compatibility.
🔧 Debug
advanced
2:00remaining
What error does this JSON parsing code produce?
What error will this JavaScript code produce when run?
Rest API
const badJson = '{name: "Bob", age: 25}';
const data = JSON.parse(badJson);
ANo error, data contains the object
BSyntaxError: Unexpected token n in JSON at position 1
CReferenceError: badJson is not defined
DTypeError: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint
Check the JSON string format carefully.
❓ Predict Output
advanced
2:30remaining
What is the output of this REST API JSON response handling code?
Given this JavaScript code snippet that fetches JSON data from an API, what will be logged?
Rest API
async function getUser() {
  const response = await fetch('https://api.example.com/user');
  const data = await response.json();
  console.log(data.active);
}

// Assume the API returns: {"id": 1, "name": "Eve", "active": false}
getUser();
Afalse
BSyntaxError
Cundefined
Dtrue
Attempts:
2 left
💡 Hint
Look at the value of the 'active' property in the JSON response.
🚀 Application
expert
2:00remaining
How many keys are in this JSON object after parsing?
Consider this JSON string and the code that parses it.

How many keys does the resulting object have at the top level?
Rest API
const jsonStr = '{"user": {"id": 10, "name": "Sam"}, "status": "active", "roles": ["admin", "user"]}';
const obj = JSON.parse(jsonStr);
console.log(Object.keys(obj).length);
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Count only the keys at the top level of the object.

Practice

(1/5)
1. What is the main reason REST APIs use JSON as their standard format?
easy
A. Because JSON is easy to read and write for both humans and machines
B. Because JSON requires special software to understand
C. Because JSON is a binary format
D. Because JSON only supports numbers

Solution

  1. Step 1: Understand JSON format

    JSON is a text-based format that uses simple syntax with objects and arrays.
  2. Step 2: Identify why REST APIs prefer JSON

    REST APIs use JSON because it is easy for humans to read and easy for machines to parse.
  3. Final Answer:

    Because JSON is easy to read and write for both humans and machines -> Option A
  4. Quick Check:

    JSON is text-based and readable [OK]
Hint: JSON is popular because it's simple and readable [OK]
Common Mistakes:
  • Thinking JSON is binary
  • Believing JSON needs special software
  • Assuming JSON only supports numbers
2. Which of the following is the correct JSON syntax for an object with a key "name" and value "Alice"?
easy
A. {"name" = "Alice"}
B. {name: "Alice"}
C. ["name": "Alice"]
D. {"name": "Alice"}

Solution

  1. Step 1: Recall JSON object syntax

    JSON objects use curly braces with keys and values as strings, keys must be in double quotes.
  2. Step 2: Check each option

    {"name": "Alice"} uses double quotes correctly around key and value, with colon separator.
  3. Final Answer:

    {"name": "Alice"} -> Option D
  4. Quick Check:

    Keys and strings in double quotes [OK]
Hint: Keys and string values must be in double quotes [OK]
Common Mistakes:
  • Omitting quotes around keys
  • Using square brackets for objects
  • Using equals sign instead of colon
3. Given this JSON response from a REST API:
{"status": "success", "data": [10, 20, 30]}

What is the value of the key "data"?
medium
A. "success"
B. [10, 20, 30]
C. 10
D. {"status": "success"}

Solution

  1. Step 1: Identify the key "data" in JSON

    The key "data" holds an array with values 10, 20, and 30.
  2. Step 2: Match the value to the options

    [10, 20, 30] correctly shows the array [10, 20, 30].
  3. Final Answer:

    [10, 20, 30] -> Option B
  4. Quick Check:

    Key "data" = array [10, 20, 30] [OK]
Hint: Look for the key and read its value exactly as shown [OK]
Common Mistakes:
  • Confusing key "status" value with "data"
  • Picking a single number instead of the array
  • Choosing the whole JSON object as value
4. You receive this JSON string from an API:
{"user": "Bob", "age": 25,}

What is wrong with this JSON?
medium
A. Trailing comma after the last item is invalid
B. Keys must be numbers, not strings
C. Values cannot be numbers
D. Missing square brackets around the object

Solution

  1. Step 1: Check JSON syntax rules

    JSON objects cannot have a comma after the last key-value pair.
  2. Step 2: Identify the error in the string

    The comma after "age": 25 is invalid and causes a syntax error.
  3. Final Answer:

    Trailing comma after the last item is invalid -> Option A
  4. Quick Check:

    No trailing commas allowed in JSON objects [OK]
Hint: No commas after last item in JSON objects [OK]
Common Mistakes:
  • Thinking keys must be numbers
  • Believing numbers are invalid values
  • Adding brackets incorrectly
5. You want to send a JSON payload to a REST API with user info: name "Eve", age 28, and hobbies as an array of "reading" and "swimming". Which JSON is correct?
hard
A. {"name": "Eve", "age": 28, "hobbies": "reading, swimming"}
B. {"name": Eve, "age": "28", "hobbies": ("reading", "swimming")}
C. {"name": "Eve", "age": 28, "hobbies": ["reading", "swimming"]}
D. {"name": "Eve", "age": 28, "hobbies": {"reading", "swimming"}}

Solution

  1. Step 1: Check correct JSON types for each field

    Name is a string, age is a number, hobbies is an array of strings.
  2. Step 2: Validate each option's syntax

    {"name": "Eve", "age": 28, "hobbies": ["reading", "swimming"]} correctly uses double quotes for strings, numbers without quotes, and square brackets for array.
  3. Final Answer:

    {"name": "Eve", "age": 28, "hobbies": ["reading", "swimming"]} -> Option C
  4. Quick Check:

    Strings in quotes, numbers plain, arrays in brackets [OK]
Hint: Use quotes for strings, brackets for arrays, no quotes for numbers [OK]
Common Mistakes:
  • Using parentheses instead of brackets for arrays
  • Not quoting string values
  • Putting numbers in quotes unnecessarily
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