0
0
Rest APIprogramming~20 mins

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

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