res.json()

Sends a JSON response, serializing the value and setting the content type.

Since Express 4 Spec ↗

Syntax

res.json(body)

Parameters

NameTypeRequiredDescription
body any Yes The value to serialize with JSON.stringify.

Returns

Response — The response object.

Examples

app.get('/users/:id', (req, res) => {
  res.json({ id: req.params.id, name: 'Ada' });
});
Output
{"id":"1","name":"Ada"}
app.get('/fail', (req, res) => {
  res.status(404).json({ error: 'not found' });
});
Output
(404) {"error":"not found"}

Notes

Sets `Content-Type: application/json` and serializes with `JSON.stringify` (respecting the `json replacer`/`json spaces` app settings). Chain `res.status(code)` before it to set a non-200 status. `undefined` properties are dropped.

See also