Express.js res.json() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

res.json() sends a JSON response. Express converts the value with JSON.stringify() and sets the correct Content-Type. You can pass an object, array, string, Boolean, number, or null.

Chain res.status() for HTTP codes — for example res.status(500).json({ error: 'message' }). For reading JSON request bodies, use the express.json() middleware. Call it once per request; after it runs, res.headersSent is true.

Syntax

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

Express
res.json([body])
res.json(null);
res.json({ user: 'tobi' });
res.status(500).json({ error: 'message' });
CallWhat it does
res.json([body])Primary signature.
res.json(null);Related call form.
res.json({ user: 'tobi' });Related call form.
res.status(500).json({ error: 'message' });Related call form.

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();

res.json([body])
res.json(null);
res.json({ user: 'tobi' });
res.status(500).json({ error: 'message' });

Parameters

ParameterDescription
bodyRequired. Value to send as JSON. Objects, arrays, strings, Booleans, numbers, and null are supported; other values are converted to JSON too.

Return Value

res.json() sends a JSON response and returns res. After it runs, the response is finished — do not send again.

Express
return res.status(201).json({ id: 42 });
// chaining uses the returned res; return avoids double-send

Express.js Examples

Example 1 — Send a Basic JSON Payload

Return a small object — the most common API reply.

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

app.get('/api/ping', (req, res) => {
  res.json({ ok: true, message: 'pong' });
});

// Also valid per docs:
// res.json(null);
// res.json({ user: 'tobi' });

app.listen(3000);

How It Works

  1. Pass a plain object. No manual JSON.stringify needed.
  2. Express stringifies. Official docs: conversion uses JSON.stringify().
  3. Content-Type is set for JSON automatically.
  4. Response finishes. Do not call send/json again on this request.

Example 2 — Set Status and Return JSON

Create a resource and answer with 201 plus the new data.

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

app.use(express.json());

app.post('/api/users', (req, res) => {
  const user = { id: 101, name: req.body.name };
  res.status(201).json({ message: 'User created', data: user });
});

app.listen(3000);

How It Works

  1. express.json() parses the request body so req.body is available.
  2. status(201) marks a successful create (official pattern uses status + json).
  3. json sends the payload with the chosen status in one chain.
  4. Clients read both the status line and the JSON body.

Example 3 — Consistent JSON Error Response

Validate input, return early with 400, otherwise return success JSON.

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

app.get('/api/orders/:id', (req, res) => {
  if (!/^\d+$/.test(req.params.id)) {
    return res.status(400).json({ error: 'Invalid order id' });
  }

  res.json({ id: Number(req.params.id), status: 'processing' });
});

// Server error style from the docs:
// res.status(500).json({ error: 'message' });

app.listen(3000);

How It Works

  1. Validate first. Non-numeric ids are rejected.
  2. return after error json. Stops the handler from sending success too.
  3. Same Content-Type for errors. Clients always parse JSON.
  4. Docs also show res.status(500).json({ error: 'message' }) for server failures.

Runtime Support

res.json() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the JSON body; they do not execute this method.

✓ Server · Express 4 & 5

res.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 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.json() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Prefer res.json for API payloads.

Frequently Asked Questions

It converts the given value to a JSON string with JSON.stringify(), sets the correct Content-Type, and sends the HTTP response.
Any JSON type: object, array, string, Boolean, number, or null. Other values are converted to JSON as well.
Chain status first: res.status(201).json(data) or res.status(500).json({ error: 'message' }).
For API JSON payloads, prefer res.json() — intent is explicit and Content-Type is set for JSON. res.send() also handles objects, but json makes the contract clearer.
res.jsonp() is the same idea with JSONP callback support for older browser cross-domain patterns. Prefer json for modern APIs.

Next: res.jsonp()

Send JSON with optional JSONP callback wrapping.

res.jsonp() 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