Skip to content
← SyntaxBench reference notes

Recover Original JSON After Pretty Printing

Contents
AI-generated illustration; not taken on location · Abstract diagram of JSON compaction
AI-generated illustration; not taken on location · Illustration of formatting being removed from JSONAI-generated illustration

Pretty-printing JSON makes it easier to read by adding indentation, line breaks, and spaces. When you need to send the JSON over a network, store it in a file, or process it with another tool, you often want to remove that extra formatting and return the document to a dense, single-line form. Reversing pretty printing is not the same as recovering the exact original bytes, because the original whitespace may have included spaces or line breaks that were never meaningful to JSON semantics. The resulting compact JSON still represents the same data when the formatting is removed correctly.

This guide explains what pretty printing changes and does not change, how to compact JSON using the syntaxbench.click JSON formatter in a browser, how Microsoft Edge and Firefox developer tools display raw or pretty-printed JSON, and how to produce compact JSON programmatically with Python’s json module. It also lists decisions and risks to check before compacting, including validation, duplicate keys, size limits, and semantic differences.

What pretty printing changes and does not change

Pretty printing reformats JSON so people can read it more easily, but reversing that process does not guarantee that you recover the exact original text. The operation changes spacing, line breaks, and indentation; it does not alter the data itself. Number tokens stay intact, escaped strings remain unchanged, key order is preserved, and duplicate keys are not removed. Large integers are never rounded during formatting. Because formatting only modifies whitespace between tokens, the underlying JSON semantics stay the same even though the raw bytes differ.

That distinction matters when you need to compare files byte-for-byte, verify checksums, or reproduce an exact payload. Compact output removes unnecessary spaces and puts the content on one dense line wrapped to the window, but it reconstructs whitespace rather than restoring the historical formatting. A compacted document may therefore be valid and equivalent without being identical to the input before it was pretty-printed.

Before relying on reversed formatting, check that the source is valid JSON: keys must be double-quoted, and comments or trailing commas are not allowed. Also remember that Python’s JSON encoder preserves input order by default unless the underlying container is unordered, so reordering is unlikely unless your data structure itself discards order.

In short, pretty printing changes how JSON looks, not what it means. Compacting it back removes the added whitespace, but it does not time-travel to the original byte sequence unless the original already used the same compact style.

Using syntaxbench.click json-format to compact JSON

The syntaxbench.click json-format tool works entirely in the browser, so you can paste formatted JSON into an editable field and generate compact output without signing in or uploading a file. Paste your formatted JSON into the source editor, then request minified output. The resulting JSON appears in a dense format, wrapped to the window, with the entire content effectively on one line and unnecessary spaces removed.

Value preservation follows the same rules as any reversible formatting change: number tokens stay intact, escaped strings remain unchanged, key order is preserved, duplicate keys stay as they were in the source, and large integers are not rounded. Formatting changes whitespace only, so compacting removes spaces and line breaks but does not alter the JSON values themselves.

Because JSON requires double-quoted keys and does not permit comments or trailing commas, verify that your pasted source is valid JSON before expecting reliable output. If you do not know the tool’s current input-size limit, nesting-depth cap, duplicate-key warning behavior, or formatted-output cutoff, check the tool’s own help or settings before pasting large or deeply nested documents.

Browser developer tools options

Microsoft Edge and Firefox offer different ways to inspect JSON in the browser. Edge’s JSON viewer can apply pretty-print formatting to make nested objects easier to read, while Firefox’s built-in JSON viewer shows syntax-highlighted JSON and lets users switch between rendered and raw text. These are primarily display actions rather than edits to an underlying file.

In Edge, opening a JSON response or file often triggers the browser’s JSON viewer. The viewer may present a Pretty-print option or similar toggle that adds indentation and line breaks so the structure is easier to scan. Clearing that option generally returns the JSON to a denser on-screen display. Because this is a viewer setting, toggling it does not rewrite the source JSON file or change the bytes received from the server. Readers who need a saved compact file should copy the displayed text after turning pretty-print off, then paste it into a text editor or compaction tool.

Firefox handles JSON through its own viewer as well. The browser can syntax-highlight JSON objects and offer a raw view that shows the document as received. If the original response was minified, viewing the raw text may already show dense JSON. If Firefox pretty-printed the document, returning to raw or view-source mode can restore the unformatted appearance. Neither step alters the underlying document unless the user explicitly saves a new version.

Firefox’s JSON viewer is designed for JSON documents rather than JSON Lines. If a JSON Lines file contains one JSON object per line, Firefox may attempt to parse the entire response as a single JSON document. Lines that are not valid JSON can cause parse failures or unexpected viewer behavior. In those cases, opening the file in a text editor is a safer way to inspect the original dense lines.

Browser inspection is useful for checking how a server formatted a response, but it is not a substitute for a dedicated minifier when a compact file is required. Developer tools can copy visible text, yet they may wrap long lines or normalize display whitespace in ways that do not match byte-for-byte compaction. For production work, verify the copied output against a JSON validator or programmatic compactor before relying on it.

Comparison points:

  • Edge: viewer pretty-print toggle affects display only; clearing it shows denser text without modifying the source file.
  • Firefox: syntax-highlighted JSON viewer with raw or view-source access; best suited to whole JSON documents, not necessarily JSON Lines.
  • Both: copy output manually if a local compact copy is needed.

Readers should check whether their target is a single JSON document or a stream of JSON objects before choosing a browser method. When the viewer’s dense display is not guaranteed to preserve every byte exactly, copying into an external editor and validating the result is the more reliable path.

Command-line and programmatic compaction

Use a programming API when you need to compact JSON reliably without depending on browser display behavior. A common approach is to parse the formatted text into an object and then serialize it again with compact separators.

In Python, call json.dumps() on the parsed object. To remove the spaces normally added after commas and colons, pass separators=(‘,’, ‘:’). This produces the most compact encoding supported by the module’s default serializer. If separators is omitted and indent is None, Python defaults to (‘, ‘, ‘: ‘), which retains spaces and therefore is not minified.

The default encoder preserves key order when the underlying container preserves order, so compaction normally does not reorder object members. Numbers, escaped strings, and duplicate keys are carried through according to the parser’s behavior rather than being rewritten or rounded.

A practical workflow is to read the pretty-printed file, load it with json.load(), then write the result with json.dumps(obj, separators=(‘,’, ‘:’)). Verify that the output parses successfully before replacing the original, especially if the source contains nested structures or large integers.

Other languages provide equivalent options, often described as minify, compact, or serialize-without-whitespace. Check the library documentation for the exact separator or flag names, because behavior varies between implementations.

Decisions and risks before compacting

Before compacting JSON, check whether the source text passes validation. JSON requires double-quoted keys and does not allow comments or trailing commas, so files that fail those rules should be fixed first rather than minified in their current state.

Duplicate keys are another concern. Formatting keeps duplicate keys intact rather than removing them, and compacting preserves that same structure. If the syntaxbench JSON formatter issues a warning about duplicate keys, review whether the application or downstream parser expects unique keys before relying on the compact output.

Size and depth limits matter for large documents. Users should verify the maximum input size in UTF-8 bytes and any nesting depth limit enforced by the syntaxbench JSON formatter. If deeply indented formatted output reaches a formatted-output cap, the compact result may still be valid but could be harder to inspect, so keep a backup of the original formatted text.

Compaction changes how JSON looks, not what it means semantically when formatting is the only difference. Number tokens, escaped strings, key order, duplicate keys, and large integers remain intact, and large integers are not rounded. However, reversing pretty printing restores dense whitespace rather than guaranteeing byte-for-byte restoration of the original file.

Use validation before compaction, keep an unmodified copy for comparison, and confirm that any target system accepts the compact format. If the input may contain non-JSON lines or mixed content, determine whether those lines should be removed or handled separately before minification.