Skip to content
Back to Blog
Developer ToolsMarch 8, 2026·7 min read

JSON Formatting Tips Every Developer Should Know

Beyond pretty-printing — learn JSON validation, tree navigation, key sorting, minification, and how to debug complex nested structures effectively.

Developer laptop with code and JSON data on screen

Pretty-printing is just the beginning. JSON (RFC 8259) rules every public API, every config file, every browser DevTools panel — and most teams never go further than a basic formatter. This guide shows the techniques that separate fluent developers from the rest: schema validation, tree-view navigation, key sorting for diffable output, minification economics, JSONPath / JMESPath queries, and the strict spec rules (no comments, no trailing commas, only double quotes) that catch 80% of production bugs.

Why Formatting Matters

An unformatted 80 KB Stripe webhook payload is one line of unreadable text. Formatted, it becomes a navigable document that reveals customer IDs, line items, metadata, and signatures at a glance. Beyond reading, consistent formatting unlocks diffing, code review, caching by content hash, and cryptographic signing (canonical form).

JSON Spec Rules That Trip Everyone Up

RuleAllowed?Note
Single-quoted stringsNoJSON requires "
Trailing commasNoJS allows; JSON doesn't
// commentsNoUse JSONC for configs
Unquoted keysNoAll keys must be "strings"
NaN, Infinity, undefinedNoUse null or omit
Hex / octal numbersNoDecimal only
Leading + on numbersNo+1 is invalid
Duplicate keysToleratedBehavior undefined; last-wins in most parsers
BOM byte order markNoMust be UTF-8 without BOM
Numbers above 2^53LossUse string IDs for large integers

🚫 Big-int trap: JavaScript's JSON.parse silently rounds integers above 2^53. Always serialize 64-bit IDs (Snowflake, Twitter, Discord) as strings.

Validation: Find Errors Instantly

Our JSON Formatter validates as you type. Common errors and their fixes:

Error messageLikely causeFix
Unexpected token } in JSONTrailing commaRemove the last comma
Unexpected token ' in JSONSingle quotesReplace with "
Unexpected end of JSON inputMissing closing bracketMatch braces/brackets
Bad control characterRaw newline in stringEscape as
Unexpected token NNaN in payloadReplace with null

Tree View: Navigate Deeply Nested Data

Once a payload exceeds ~200 lines, scrolling becomes hostile. Tree view collapses every object and array into a clickable node, lets you focus on a single subtree, and shows the type + child count at every level. It's the GraphQL Explorer experience for any JSON.

📌 In our JSON Formatter, click any object/array node to copy that exact subtree to clipboard — perfect for sharing reproducers.

Key Sorting for Diffable JSON

Two JSON objects with identical content but different key orders look completely different in a text diff. Canonical JSON (RFC 8785) defines a deterministic ordering — sort keys lexicographically at every level. Use it for:

  • Comparing staging vs production configs
  • Hashing payloads for cache keys / dedupe
  • Signing JSON cryptographically (JWS, COSE)
  • Producing stable Git diffs of generated files

Minification: Bytes vs Readability

ModeSizeUse for
Pretty (2-space)+30–50%Editing, code review
Pretty (4-space)+50–80%Documentation
MinifiedbaselineAPI responses, network
Minified + gzip−65–80%Static assets, REST/GraphQL

JSONPath, JMESPath & jq

For extracting subsets of large payloads:

LanguageExampleResult
JSONPath$.users[?(@.role=='admin')].emailAll admin emails
JMESPathusers[?role=='admin'].emailSame, AWS-flavoured
jq.users[] | select(.role=="admin") | .emailSame, CLI-friendly

Schema Validation with JSON Schema

For data contracts between services, validate against a JSON Schema. It catches typos, missing required fields, wrong types, out-of-range values, and unknown properties at the boundary — before they cause cascading failures.

{
  "type": "object",
  "required": ["id", "email"],
  "properties": {
    "id":    { "type": "string", "format": "uuid" },
    "email": { "type": "string", "format": "email" },
    "age":   { "type": "integer", "minimum": 0, "maximum": 150 }
  },
  "additionalProperties": false
}

Performance Tips for Large Files

Stream parse >100 MB files with oboe.js / stream-json instead of JSON.parse.

Lazy-render the tree — only expand visible nodes (virtualized lists).

Use NDJSON (newline-delimited) for log streams instead of one giant array.

Compress on the wire — Brotli or gzip cuts payloads 5–10× with zero code change.

Common Mistakes

MistakeSymptomFix
Using JSON for configs with commentsCan't document choicesSwitch to JSONC, JSON5, TOML, or YAML
Returning numbers for IDsPrecision loss above 2^53Always strings for large IDs
JSON.stringify with DateLoses timezoneSerialize as ISO 8601 strings
Stringifying circular referencesTypeErrorUse a replacer function or break the cycle
Escaping unicode unnecessarilyé instead of éOutput raw UTF-8

Tools

Frequently Asked Questions

Why doesn't JSON support comments?

Douglas Crockford intentionally removed them so JSON would not be used as a programming language. For commented configs, use JSONC, JSON5, or YAML.

Are duplicate keys legal?

The spec discourages them but does not forbid them. Most parsers keep the last value. Don't rely on either behavior.

How do I represent dates in JSON?

Use ISO 8601 strings such as "2026-05-02T14:30:00Z". There is no native date type.

Why does my 19-digit ID lose digits?

JavaScript numbers are IEEE-754 doubles, accurate only up to 2^53. Serialize big IDs as strings.

Should I sort keys before signing JSON?

Yes — use canonical JSON (RFC 8785). Otherwise the same payload produces different signatures.

What is NDJSON?

Newline-delimited JSON: one valid JSON value per line. Ideal for logs, streaming, and append-only data because each line can be parsed independently.


References

🚀 Free ToolZilla tools used in this article

All client-side, no signup, no upload — open them in a new tab while you read:


Mastering JSON is less about syntax and more about discipline: validate at every boundary, sort keys for diffable output, minify for the wire, stringify big IDs, and reach for tree view or jq the moment a payload outgrows your screen. Pretty-printing is the first 10% — the rest is where the real productivity lives.

Continue Reading

Related Articles

Free & Private

Explore Our Free Tools

40+ browser-based utilities — fast, private, and always free. No sign-up required.

Browse All Tools