0
0
Expressframework~10 mins

res.json for JSON responses in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - res.json for JSON responses
Client sends HTTP request
Express route handler receives request
Handler prepares JavaScript object
Call res.json(object)
Express converts object to JSON string
Express sets Content-Type: application/json
Express sends JSON response to client
Client receives and parses JSON
This flow shows how Express takes a JavaScript object, converts it to JSON, sets the right headers, and sends it back to the client.
Execution Sample
Express
app.get('/data', (req, res) => {
  const info = { name: 'Alice', age: 30 };
  res.json(info);
});
This code sends a JSON response with a simple object when the '/data' route is requested.
Execution Table
StepActionInput/StateOutput/Result
1Client sends GET request to '/data'Request: GET /dataRequest received by Express
2Route handler runsNo special inputCreates object { name: 'Alice', age: 30 }
3Call res.json(info)info = { name: 'Alice', age: 30 }Express converts object to JSON string
4Express sets headerContent-Typeapplication/json
5Express sends responseJSON stringClient receives JSON response
6Client parses JSONJSON stringJavaScript object { name: 'Alice', age: 30 }
7EndResponse sentNo further action
💡 Response sent and connection closed after JSON data delivered
Variable Tracker
VariableStartAfter Step 2After Step 3Final
infoundefined{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }
res.headersSentfalsefalsetruetrue
Key Moments - 3 Insights
Why do we use res.json() instead of res.send() for JSON data?
res.json() automatically converts the object to a JSON string and sets the Content-Type header to 'application/json', ensuring the client knows the response is JSON. See execution_table step 4.
What happens if you pass a non-object (like a string) to res.json()?
res.json() will convert it to a JSON string anyway, for example, a string becomes a JSON string with quotes. This is shown in execution_table step 3 where conversion happens.
Can you call res.json() multiple times in one request?
No, once the response is sent (res.headersSent becomes true), calling res.json() again will cause an error. See variable_tracker for res.headersSent change.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Content-Type header set to at step 4?
Atext/plain
Btext/html
Capplication/json
Dapplication/javascript
💡 Hint
Check the 'Action' and 'Output/Result' columns at step 4 in execution_table
At which step does Express convert the JavaScript object to a JSON string?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column describing conversion in execution_table
If you tried to call res.json() again after step 5, what would happen?
AIt would send another JSON response successfully
BIt would throw an error because headers are already sent
CIt would overwrite the previous response
DIt would silently ignore the second call
💡 Hint
Refer to variable_tracker row for res.headersSent and key_moments about multiple calls
Concept Snapshot
res.json(object) sends a JSON response.
It converts the object to a JSON string.
Sets Content-Type to application/json.
Sends the response to the client.
Use it to send data in JSON format easily.
Full Transcript
When a client sends a request to an Express server route, the route handler can prepare a JavaScript object to send back. Calling res.json(object) converts this object into a JSON string, sets the Content-Type header to 'application/json', and sends the response. The client then receives this JSON string and can parse it back into an object. This method ensures the response is correctly formatted as JSON and the client knows how to handle it. Calling res.json() more than once per request is not allowed because the response can only be sent once.