Express.js express.json() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
express()

Definition

In Express.js, express.json() is built-in middleware that parses incoming requests with a JSON body and stores the result on req.body. It only looks at requests whose Content-Type matches the type option (default application/json).

Use it for APIs that accept JSON from fetch, mobile apps, or other servers. Register it with app.use() before routes that read the body. It runs on your Express server — not in the browser.

Syntax

Call express.json in your Express.js app. Start with this line.

Express
express.json([options])
CallWhat it does
express.json([options])Primary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

express.json([options])

Parameters

ParameterDescription
optionsOptional. Parsing settings. Omit for defaults.
options.limitOptional. Max body size. Default '100kb'. Prefer keeping this modest to reduce memory pressure.
options.typeOptional. Which Content-Type values to parse. Default 'application/json'.
options.strictOptional. Default true: only objects and arrays. false allows any value JSON.parse accepts (for example a bare number).
options.inflateOptional. Default true: inflate gzip/deflate bodies. false rejects compressed bodies.
options.reviverOptional. Passed to JSON.parse as the reviver (for example turn ISO strings into Date).
options.verifyOptional. verify(req, res, buf, encoding) — inspect the raw buffer before parse; throw to abort.
options.defaultCharsetOptional. Charset when the request omits one. Default 'utf-8'.

Return Value

express.json() returns a middleware function. Mount it with app.use(express.json()) (optionally with options). The return value is that middleware — not a parsed body.

Express
const jsonParser = express.json({ limit: '100kb' });
app.use(jsonParser);

// Same idea inline:
app.use(express.json());

Express.js Examples

Example 1 — Parse a JSON Body into req.body

Mount the parser, then echo the parsed object from a POST route.

Express
const express = require('express');
const app = express();

app.use(express.json());

app.post('/profile', (req, res) => {
  console.dir(req.body);
  res.json(req.body);
});

app.listen(3000);

How It Works

  1. Mount early. app.use(express.json()) runs for every request before routes.
  2. Match the type. A JSON body with Content-Type: application/json is read and parsed.
  3. Fill req.body. The object becomes available in later handlers.
  4. Respond with JSON. res.json(req.body) sends the same data back to the client.

Example 2 — Cap Payload Size with limit

Keep the default spirit of a small limit, or set an explicit cap for your API.

Express
const express = require('express');
const app = express();

app.use(express.json({ limit: '50kb' }));

app.post('/notes', (req, res) => {
  res.status(201).json({
    ok: true,
    title: req.body.title
  });
});

app.listen(3000);

How It Works

  1. Pass options. express.json({ limit: '50kb' }) rejects oversized bodies.
  2. Default is already modest. Without options, Express uses '100kb' — avoid very large limits unless you truly need them.
  3. Small bodies succeed. Valid JSON under the limit reaches req.body.
  4. Large bodies fail early. The parser stops before your route tries to process huge data.

Example 3 — Handle Invalid JSON Cleanly

Malformed JSON should return a clear 400, not a confusing crash page.

Express
const express = require('express');
const app = express();

app.use(express.json());

app.post('/items', (req, res) => {
  res.status(201).json({ item: req.body });
});

app.use((err, req, res, next) => {
  if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
    return res.status(400).json({ error: 'Invalid JSON body' });
  }
  next(err);
});

app.listen(3000);

How It Works

  1. Parser runs first. Bad JSON never becomes a usable object on req.body.
  2. Error middleware catches it. The four-argument (err, req, res, next) handler runs when parsing fails.
  3. Return a clear status. Clients get 400 and a short JSON error message.
  4. Pass other errors on. Unrelated errors call next(err) so other handlers can deal with them.

Runtime Support

express.json() is Express.js built-in middleware. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it — they only send JSON over HTTP.

✓ Server · Express 4.16+ & 5

express.json()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
express.json() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It is built-in middleware that reads a JSON request body when Content-Type matches (default application/json), parses it, and sets req.body to the result.
Call app.use(express.json()) before any route that reads req.body. If you mount it after those routes, req.body will be empty or undefined.
express.json() parses JSON (typical for APIs and fetch). express.urlencoded() parses HTML form bodies (application/x-www-form-urlencoded). Many apps use both.
The parser fails and passes an error to Express. Without error middleware you may get a generic failure; with a four-argument error handler you can return a clear 400 JSON response.
No. It runs on your Node.js (or compatible) server. Browsers only send the HTTP request with a JSON body.

Next: express.Router()

Create modular route stacks with the Express Router factory.

express.Router() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful