Parse API request bodies
An API endpoint receives application/json payloads and needs JavaScript data in its handler.
Express.js · Function
express.json([options]) returns built-in middleware that parses matching JSON request bodies and exposes the parsed value as req.body. Options configure size limits, media-type matching, decompression, strictness, transformation, and verification.
Calling express.json(options) creates middleware that examines requests whose Content-Type matches the configured type, parses their JSON body, and places the result on req.body. It also handles supported compressed encodings and can constrain parsing with options such as limit, strict, or verify.
express.json([options])| Name | Type | Description |
|---|---|---|
options | Object | undefined | Optional configuration object controlling JSON parsing, including inflate, limit, reviver, strict, type, and verify. |
Middleware function that parses matching JSON request bodies and populates req.body.
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);This application parses matching JSON request bodies before the route handler and returns the parsed value.
An API endpoint receives application/json payloads and needs JavaScript data in its handler.
A service must reject unusually large JSON payloads.
An endpoint accepts a vendor-specific JSON media type.
express.json() returns middleware that parses incoming requests with JSON payloads. It only examines requests whose Content-Type matches the configured type and assigns the parsed result to req.body.
Follow-up: How can the accepted media type be customized? Pass the type option as a string, an array of strings, or a function.
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: What happens when a request exceeds the limit? The parser rejects the oversized body rather than passing it to the route handler.
The strict option controls whether only arrays and objects are accepted. When strict mode is disabled, values accepted by JSON.parse can also be parsed.
Follow-up: Which option controls the media-type match? type does; its default is application/json.
express.urlencoded()express.raw()express.text()req.bodyapp.use()