Member-only story

Indenting JSON.stringify’s Output

Oliver Jumpertz
3 min readJan 25, 2022

You may already have used JSON.stringify a few times already. There’s nearly no better way to quickly log your JavaScript objects and take a look if they have the structure you expect them to have. But did you know that you can customize and control the indentation of the output?

Usual Usage

You can usually use JSON.stringify like this:

const obj = {
propertyOne: 1,
propertyTwo: “2”,
propertyThree: {
nestedPropertyOne: 1.123
}
};
console.log(JSON.stringify(obj));
// prints => {"propertyOne":1,"propertyTwo":"2","propertyThree":{"nestedPropertyOne":1.123}}

It works perfectly fine, but the larger your objects are, the harder reading the output becomes. There must be something to help you with making larger objects readable again.

JSON.stringify’s Syntax

Let’s take a look at what JSON.stringify actually offers.

Syntax

JSON.stringify(value[, replacer[, space]])

Parameters

value

This is the value to convert to a JSON string. You’ll always need this one.

replacer

Oliver Jumpertz
Oliver Jumpertz

Written by Oliver Jumpertz

Software Engineer - Content Creator

No responses yet

Write a response