Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Function

json

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.

Since
4.16.0

How it works

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.

Syntax

express.json([options])

Parameters

NameTypeDescription
optionsObject | undefinedOptional configuration object controlling JSON parsing, including inflate, limit, reviver, strict, type, and verify.

Returns

Middleware function that parses matching JSON request bodies and populates req.body.

Key points

  • express.json is included in Express, so no separate body-parser installation is needed for this API.
  • The returned middleware filters requests by Content-Type before parsing.
  • A reviver function can transform values while JSON text is converted into JavaScript data.
  • The type option supports one media-type pattern, several patterns, or custom matching logic.

Common mistakes

  • The parsed value is available as req.body only after this middleware has run.
  • Compressed gzip and deflate bodies are automatically inflated unless inflation is disabled.

Example: Parse JSON request bodies

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.

When to use it

Parse API request bodies

An API endpoint receives application/json payloads and needs JavaScript data in its handler.

Enforce request-size limits

A service must reject unusually large JSON payloads.

Customize media-type matching

An endpoint accepts a vendor-specific JSON media type.

Interview questions

QWhat does express.json() do?

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.

QHow does the limit option work?

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.

QWhat is the purpose of strict?

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.body
  • app.use()

Source: Express Object · Express.js 4.x. Checked 2026-10-15.