res.json() sends a JSON response. Express converts the value with JSON.stringify() and sets the correct Content-Type. You can pass an object, array, string, Boolean, number, or null.
Chain res.status() for HTTP codes — for example res.status(500).json({ error: 'message' }). For reading JSON request bodies, use the express.json() middleware. Call it once per request; after it runs, res.headersSent is true.
Syntax
Call res.json in your Express.js app. Start with this line.
POST /api/users
Content-Type: application/json
{ "name": "Ada" }
→ 201 Created
→ {"message":"User created","data":{"id":101,"name":"Ada"}}
How It Works
express.json() parses the request body so req.body is available.
status(201) marks a successful create (official pattern uses status + json).
json sends the payload with the chosen status in one chain.
Clients read both the status line and the JSON body.
Example 3 — Consistent JSON Error Response
Validate input, return early with 400, otherwise return success JSON.
Express
const express = require('express');
const app = express();
app.get('/api/orders/:id', (req, res) => {
if (!/^\d+$/.test(req.params.id)) {
return res.status(400).json({ error: 'Invalid order id' });
}
res.json({ id: Number(req.params.id), status: 'processing' });
});
// Server error style from the docs:
// res.status(500).json({ error: 'message' });
app.listen(3000);
Output
GET /api/orders/abc
→ 400 { "error": "Invalid order id" }
GET /api/orders/42
→ 200 { "id": 42, "status": "processing" }
How It Works
Validate first. Non-numeric ids are rejected.
return after error json. Stops the handler from sending success too.
Same Content-Type for errors. Clients always parse JSON.
Docs also showres.status(500).json({ error: 'message' }) for server failures.
Compatibility
Runtime Support
res.json() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the JSON body; they do not execute this method.
✓ Server · Express 4 & 5
res.json()
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.
ServerNode.js runtime API
Node.js18+ for Express 5 · Express 4 supported
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.json()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Prefer res.json for API payloads.
Frequently Asked Questions
It converts the given value to a JSON string with JSON.stringify(), sets the correct Content-Type, and sends the HTTP response.
Any JSON type: object, array, string, Boolean, number, or null. Other values are converted to JSON as well.
Chain status first: res.status(201).json(data) or res.status(500).json({ error: 'message' }).
For API JSON payloads, prefer res.json() — intent is explicit and Content-Type is set for JSON. res.send() also handles objects, but json makes the contract clearer.
res.jsonp() is the same idea with JSONP callback support for older browser cross-domain patterns. Prefer json for modern APIs.