Bird
Raised Fist0
Expressframework~20 mins

res.json for JSON responses in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express JSON Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Express route?
Consider this Express route handler:
app.get('/data', (req, res) => { res.json({ message: 'Hello World' }); });

What will the client receive when requesting '/data'?
Express
app.get('/data', (req, res) => { res.json({ message: 'Hello World' }); });
AA 404 Not Found error
BA JSON response with Content-Type 'application/json' and body {"message":"Hello World"}
CAn HTML page displaying the text 'Hello World'
DA plain text response with body 'Hello World'
Attempts:
2 left
💡 Hint
res.json sends a JSON response with the correct content type.
📝 Syntax
intermediate
2:00remaining
Which option correctly sends a JSON response with a status code 201?
You want to send a JSON response with status code 201 and body { success: true }. Which code is correct?
Ares.status(201).json({ success: true });
Bres.sendStatus(201).json({ success: true });
Cres.json(201, { success: true });
Dres.json({ success: true }).status(201);
Attempts:
2 left
💡 Hint
Set status before sending JSON.
🔧 Debug
advanced
2:00remaining
What error does this code cause?
Examine this Express route:
app.get('/test', (req, res) => { res.json('Hello'); res.json({ done: true }); });

What happens when a client requests '/test'?
Express
app.get('/test', (req, res) => { res.json('Hello'); res.json({ done: true }); });
ASends only the first JSON response 'Hello' and ignores the second
BSends a combined JSON response with both values
CThrows an error because res.json is called twice on the same response
DSends 'Hello' as JSON and then sends { done: true } as JSON successfully
Attempts:
2 left
💡 Hint
You cannot send multiple responses for one request.
🧠 Conceptual
advanced
2:00remaining
How does res.json handle JavaScript objects?
When you pass a JavaScript object to res.json, what does Express do internally before sending the response?
AIt converts the object to a JSON string and sets Content-Type to 'application/json'
BIt sends the object as is without conversion
CIt converts the object to XML format
DIt converts the object to a plain text string
Attempts:
2 left
💡 Hint
Think about how JSON responses are sent over HTTP.
❓ state_output
expert
2:00remaining
What is the final response body and status code?
Given this Express route:
app.get('/status', (req, res) => { const data = { ok: true }; res.status(202); res.json(data); });

What will the client receive?
Express
app.get('/status', (req, res) => { const data = { ok: true }; res.status(202); res.json(data); });
AStatus code 500 Internal Server Error
BStatus code 200 with JSON body {"ok":true}
CStatus code 202 with empty body
DStatus code 202 with JSON body {"ok":true}
Attempts:
2 left
💡 Hint
Calling res.status sets the status for the next response method.

Practice

(1/5)
1. What does res.json() do in an Express app?
easy
A. Parses JSON data from a file
B. Reads JSON data from the client request
C. Sends a JSON response to the client with proper headers
D. Logs JSON data to the server console

Solution

  1. Step 1: Understand the purpose of res.json()

    This method is used to send JSON data back to the client in an HTTP response.
  2. Step 2: Recognize header setting and formatting

    It automatically sets the Content-Type header to 'application/json' and converts the JavaScript object to JSON format.
  3. Final Answer:

    Sends a JSON response to the client with proper headers -> Option C
  4. Quick Check:

    res.json() sends JSON response [OK]
Hint: Remember: res.json sends data, not reads it [OK]
Common Mistakes:
  • Confusing res.json with reading client data
  • Thinking it logs data instead of sending
  • Assuming it parses files automatically
2. Which of the following is the correct syntax to send a JSON response with Express?
easy
A. res.json({ message: 'Hello' })
B. res.sendJson({ message: 'Hello' })
C. res.jsonify({ message: 'Hello' })
D. res.send({ json: 'Hello' })

Solution

  1. Step 1: Identify the correct method name

    The Express method to send JSON is res.json(), not res.sendJson() or res.jsonify().
  2. Step 2: Check the argument format

    The argument should be a JavaScript object, like { message: 'Hello' }.
  3. Final Answer:

    res.json({ message: 'Hello' }) -> Option A
  4. Quick Check:

    Correct method is res.json() [OK]
Hint: Use res.json() exactly, no extra letters [OK]
Common Mistakes:
  • Using res.sendJson or res.jsonify which don't exist
  • Passing string instead of object
  • Mixing res.send with JSON method
3. What will the client receive when this Express route runs?
app.get('/data', (req, res) => {
  res.json({ success: true, count: 5 });
});
medium
A. success: true, count: 5
B. ["success", true, "count", 5]
C. An error because res.json needs a string
D. {"success":true,"count":5}

Solution

  1. Step 1: Understand what res.json sends

    It converts the JavaScript object into a JSON string with keys and values properly quoted.
  2. Step 2: Check the exact JSON format

    The object { success: true, count: 5 } becomes the JSON string '{"success":true,"count":5}'.
  3. Final Answer:

    {"success":true,"count":5} -> Option D
  4. Quick Check:

    res.json sends JSON string [OK]
Hint: res.json sends JSON string with quotes around keys [OK]
Common Mistakes:
  • Thinking it sends array format
  • Assuming it sends plain text without quotes
  • Believing res.json requires string input
4. Identify the error in this Express code snippet:
app.get('/info', (req, res) => {
  res.json = { name: 'Alice' };
});
medium
A. Missing parentheses after res.json()
B. res.json is assigned an object instead of called as a function
C. res.json cannot send objects, only strings
D. Route path should be '/info/' with trailing slash

Solution

  1. Step 1: Check how res.json is used

    The code assigns an object to res.json instead of calling it as a function.
  2. Step 2: Understand correct usage

    res.json is a method and should be called like res.json({ name: 'Alice' });
  3. Final Answer:

    res.json is assigned an object instead of called as a function -> Option B
  4. Quick Check:

    res.json must be called, not assigned [OK]
Hint: res.json is a function, always call it with () [OK]
Common Mistakes:
  • Assigning to res.json instead of calling it
  • Forgetting parentheses after res.json
  • Thinking res.json only accepts strings
5. You want to send a JSON response with a list of users but only include users with active status true. Which code correctly uses res.json to do this?
const users = [
  { id: 1, active: true },
  { id: 2, active: false },
  { id: 3, active: true }
];

app.get('/active-users', (req, res) => {
  // Fill in here
});
hard
A. res.json(users.filter(user => user.active));
B. res.json(users.map(user => user.active));
C. res.json(users);
D. res.json(users.filter(user => user.id));

Solution

  1. Step 1: Filter users by active status

    Use users.filter(user => user.active) to get only users where active is true.
  2. Step 2: Send filtered list as JSON response

    Pass the filtered array to res.json() to send it properly formatted.
  3. Final Answer:

    res.json(users.filter(user => user.active)); -> Option A
  4. Quick Check:

    Filter active users, then send JSON [OK]
Hint: Filter first, then send with res.json() [OK]
Common Mistakes:
  • Using map instead of filter, sending booleans only
  • Sending full users list without filtering
  • Filtering by id instead of active status
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