Make JSON responses easier to inspect manually
Development APIs
Express.js · Const
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.
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.
app.set('json spaces', value)| Name | Type | Description |
|---|---|---|
value | Number | String | Indentation value passed as JSON.stringify’s space argument; typically the number of spaces or an indentation string. |
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.
Development APIs
Snapshot or fixture generation
Human-facing endpoints
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.
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.
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.stringifyjson escape