Parse API request bodies
API endpoints receiving application/json payloads need parsed objects available to route handlers.
Express.js · Function
Creates Express middleware for parsing JSON request bodies. Matching parsed data is placed on req.body, with configurable size, media-type, inflation, and parsing behavior.
Call express.json() to create middleware that parses request bodies whose Content-Type matches its configured type, then places the parsed value on req.body. It accepts Unicode input and automatically inflates gzip or deflate content; requests without a body, with a nonmatching type, or with a parsing error leave req.body as an empty object.
express.json([options])| Name | Type | Description |
|---|---|---|
options | Object | undefined | Optional parser configuration. Supported settings include inflate, limit, reviver, strict, type, and verify. |
Middleware that parses matching JSON request bodies and populates req.body with the parsed data.
const express = require('express');
const app = express();
app.use(express.json({ limit: '1mb' }));
app.post('/profile', (req, res) => {
res.json({ received: req.body });
});
app.listen(3000);The middleware parses matching JSON request bodies before the route handler, making the parsed value available as req.body.
API endpoints receiving application/json payloads need parsed objects available to route handlers.
Endpoints need a smaller or larger maximum body size than the default.
Applications accept vendor-specific JSON media types or need custom JSON.parse transformations.
express.json() returns middleware that parses JSON request bodies and assigns the parsed result to req.body. By default, it considers requests whose Content-Type is application/json.
Follow-up: How can parsing be restricted to another media type? Pass a type option, such as { type: 'application/*+json' }, so matching is delegated to the configured media-type test.
The limit option controls the maximum request-body size. A numeric value is interpreted as bytes, while a string is interpreted by the bytes library.
Follow-up: Why configure a limit? A bounded body size reduces exposure to unexpectedly large payloads; choose a value appropriate for the endpoint rather than relying blindly on the default.
The middleware automatically handles gzip and deflate encodings by inflating them before parsing. Set inflate to false when compressed bodies should be rejected.
Follow-up: What happens when the request has a nonmatching Content-Type? The JSON parser skips it, and the documentation says req.body becomes an empty object when the type is not matched.
express.urlencoded()express.raw()express.text()app.use()req.bodybody-parser