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

Express.js · Const

json spaces

The json spaces application setting controls the indentation used when Express formats JSON. Set it to a number or string, typically a space count for prettified responses.

How it works

Set this application property to control the indentation used when Express serializes JSON responses. Express passes the value as the space argument to JSON.stringify, so a number or string can define the formatting.

Syntax

app.set('json spaces', value)

Parameters

NameTypeDescription
valueNumber | StringIndentation value passed as JSON.stringify’s space argument; typically the number of spaces or an indentation string.

Key points

  • The setting accepts either a numeric indentation width or a string indentation value.
  • Express delegates the formatting parameter to JavaScript’s JSON serialization function.
  • A numeric value conventionally represents the indentation width for readable JSON output.
  • The documentation lists no concrete default value; the setting is undefined until application code assigns one.

Common mistakes

  • This setting affects JSON serialization formatting; it is not a response-body size or transport setting.
  • The setting is undefined unless configured, so do not assume a pretty-printing width.

Example: Indent JSON responses

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

app.set('json spaces', 2);
app.get('/status', (req, res) => {
  res.json({ok: true, service: 'api'});
});

app.listen(3000);

The response is formatted with two-space indentation.

When to use it

Make JSON responses easier to inspect manually

Development APIs

Produce consistently indented JSON output for readable artifacts

Snapshot or fixture generation

Improve readability when clients display raw API responses

Human-facing endpoints

Interview questions

QWhat does the json spaces setting control?

json spaces is an Express application setting whose value becomes the space argument for JSON.stringify. It is commonly assigned a number to indent prettified JSON.

Follow-up: What other value can it accept? A string is also supported, because the documented type is Number | String.

QHow would you configure two-space JSON indentation?

Configure it with app.set('json spaces', 2). JSON responses serialized by Express then use that formatting preference.

Follow-up: What happens if it is not configured? The documented default is undefined, so the application does not explicitly request an indentation value.

QIs json spaces an incoming JSON parser option?

The setting is documented as an application property, and its purpose is formatting JSON generated through Express serialization. It does not parse incoming request bodies.

Follow-up: Which middleware is relevant for incoming JSON instead? Use the separately documented express.json() middleware for parsing JSON request payloads.

  • app.set()
  • app.get()
  • res.json()
  • JSON.stringify
  • json escape

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