JSON Spacing Adder

A custom JSON printer (not a JSON.stringify wrapper): any object or array whose immediate children are all scalars renders inline on one line with padded brackets (e.g. { "a": 1, "b": 2 }), while anything containing a nested object or array expands into a normal indented multi-line block, applying the same rule recursively at every level. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-24

Overview

Introduction

Standard JSON pretty-printing puts every key on its own line, which is precise but can make simple flat records take up far more vertical space than they need.

This tool takes a different approach: small, purely-scalar objects and arrays collapse onto one compact line, while genuinely nested structure still gets full multi-line indentation.

What Is JSON Spacing Adder?

A hand-written recursive JSON printer, not a thin wrapper around JSON.stringify's built-in indent option, that decides per-container whether to render inline or expanded.

The decision is purely structural: an object or array with only scalar immediate children goes on one line; anything with a nested object or array child expands, and this rule reapplies at every level of nesting.

How JSON Spacing Adder Works

The tool parses the input, then recursively formats each value: for an object or array, it first checks whether every immediate child is a scalar (isScalar returns true for anything that isn't an object).

If so, it renders that container inline with JSON.stringify per entry, space-padded inside the brackets; otherwise it renders a normal indented multi-line block, recursing the same inline-or-expand decision into each child.

When To Use JSON Spacing Adder

Use it on documents that mix simple flat records with a few genuinely nested fields, where full pretty-printing wastes space on the simple parts.

It's also a nice middle ground between minified (unreadable) and fully expanded (verbose) JSON for sharing in chat or documentation.

Features

Advantages

  • Keeps simple, flat objects and arrays compact without sacrificing readability of deeply nested parts.
  • Applies the inline/expand decision consistently and recursively at every level.
  • Produces standard, valid JSON, just with different whitespace than a typical pretty-printer.

Limitations

  • There's no way to force a specific container to stay expanded or inline; the rule is purely structural (all-scalar children or not).
  • Very wide inline objects (many scalar keys) can still produce a long single line, since there's no line-length wrapping.

Examples

Mixing inline and expanded blocks

Input

{"id":1,"name":"Ada","address":{"city":"London","zip":"E1"}}

Output

{
  "id": 1,
  "name": "Ada",
  "address": { "city": "London", "zip": "E1" }
}

The root object has a nested address object, so it expands; address itself has only scalar children, so it renders inline.

Best Practices & Notes

Best Practices

  • Use this when readability of nested structure matters more than a uniform indentation style throughout the document.
  • For strictly uniform multi-line formatting regardless of content, use JSON Formatter instead.
  • Combine with Sort JSON Object Keys first if you also want a deterministic key order before spacing.

Developer Notes

The isScalar() check treats anything that isn't typeof "object" (or is null) as scalar, so the inline-vs-expand decision is a simple Array.every() over an object's values or an array's items; this keeps the recursive format() function small since it only needs one structural branch point per container instead of separate logic paths.

JSON Spacing Adder Use Cases

  • Producing compact, readable JSON for documentation or chat messages
  • Formatting API response samples where most fields are flat but a few are nested
  • Getting a quick visual sense of which parts of a document are actually nested

Common Mistakes

  • Expecting the same output as a standard indent:2 pretty-printer; the inline collapsing is a deliberately different, more compact style.
  • Assuming a container can be forced to expand or collapse regardless of its children; the rule is always structural, not configurable per-key.

Tips

  • If a container you expected to be inline isn't, check whether one of its children is an empty object/array (still expands it since it's technically a container).
  • Use the indent option to control the multi-line block's indent width without affecting which containers go inline.

References

Frequently Asked Questions