express.json()
Built-in middleware that parses incoming JSON request bodies into req.body.
Syntax
express.json([options]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | object | No | `limit` (max body size), `strict`, `type`, `verify`, `reviver`. |
Returns
function — A middleware function.
Examples
app.use(express.json({ limit: '100kb' }));
app.post('/echo', (req, res) => res.json(req.body));
Output
$ curl -X POST -d '{"a":1}' -H 'Content-Type: application/json' localhost:3000/echo
{"a":1}
app.use(express.json());
app.use((err, req, res, next) => {
if (err.type === 'entity.parse.failed') {
return res.status(400).json({ error: 'invalid JSON' });
}
next(err);
});
Output
(400) {"error":"invalid JSON"}
Notes
Only parses requests whose `Content-Type` is JSON, leaving others
untouched. Always set a `limit` to prevent large-payload
denial-of-service. Malformed JSON triggers a 400 via the error
pipeline - handle it with error middleware.