Format JSON in Sublime Text
A practical review of formatting JSON in Sublime Text: what pretty-printing changes, when meaning is preserved, where it can alter data, and when compact output is better.
Sublime Text is a general-purpose editor, so formatting JSON in it usually means adding a formatting step: a community package installed through Package Control, or an external command run on the file or selection. This review covers what that step changes, when it preserves the data, and when you should keep the text as it is. No product-specific test results were available for this review. Statements about specific packages describe their general approach, and you should confirm commands, shortcuts and options in the documentation for the version you install.
Is formatting JSON in Sublime Text a good fit?
For reading, reviewing and editing JSON by hand, pretty-printing is usually the right choice. Indentation and line breaks make nesting visible, which makes it easier to spot a misplaced bracket or a value at the wrong level. A formatter that parses the document first also works as a validator: if the JSON is malformed, the formatting step fails, and the error usually points to the problem.
The main tradeoff is that most formatters do more than add whitespace. They parse the document and then write it out again. That rewrite can change how some values are represented, and it can reject input that is not strict JSON. In most cases the data still means the same thing, but this is not guaranteed for every input.
Leave the text unformatted in these cases:
- When the exact bytes matter, for example a payload that is signed, hashed or compared character by character.
- When the file is JSON with comments or trailing commas, such as many editor and tool configuration files. A strict parser will reject these files or remove the comments.
- When compact output is required, for example for transport, embedding in a single-line log entry, or a system that expects minified input.
- When a diff should show only real changes. Reformatting a whole file that has never been formatted creates a large whitespace-only diff.
What formatting actually changes
Formatting is a stored character change, not a display effect. The formatter inserts or removes spaces, tabs and newlines in the file. Sublime Text does not apply indentation as a visual layer on top of unchanged text. Once you save, the file contains the new whitespace.
In this context, terms like "reverse," "mirror" or "font" do not apply. Formatting JSON does not reorder characters, substitute look-alike Unicode glyphs, or change the typeface. Syntax highlighting is a separate step. Setting the view's syntax to JSON colors the tokens on screen without changing any stored characters.
The JSON grammar allows whitespace between tokens and gives it no meaning, so changing it alone does not change the data. The example below shows a compact input and a pretty-printed version with four-space indentation. The two contain the same keys, values and nesting. Only whitespace outside strings differs, and the space inside the string "Ada Lovelace" is unchanged.
{"id":7,"name":"Ada Lovelace","tags":["math","notes"],"active":true}
{
"id": 7,
"name": "Ada Lovelace",
"tags": [
"math",
"notes"
],
"active": true
}
Because most formatters parse and re-serialize, these side effects can occur depending on the tool and its settings:
- Key order: some formatters can sort keys alphabetically. JSON objects are formally unordered, but people and some tools rely on the original order.
- Duplicate keys: the JSON standard does not define how to handle repeated keys within one object. Many parsers keep only the last value, so a duplicate can disappear without warning.
- Number representation: a parser may convert numbers to its own numeric type and write them back differently. For example, an exponent form may be expanded, or very large or very precise values may lose precision.
- Non-ASCII text: some serializers escape characters outside ASCII as \u sequences by default. The value is equivalent, but it becomes harder to read.
- Indentation style: tabs and spaces, and indent width, depend on the formatter's settings, not on the editor's indentation settings.
Python's standard library shows these behaviors in a way you can check yourself. Its json.tool module pretty-prints a file. By default it escapes non-ASCII characters, and it accepts flags for key sorting and for keeping non-ASCII characters as they are. Recent Python versions support the second flag, so check the help output of your installed version. You can run it from a terminal or set it up as a Sublime Text build system:
python3 -m json.tool --indent 4 input.json
python3 -m json.tool --sort-keys --no-ensure-ascii input.json
Indentation commands built into the editor, such as reindenting lines, adjust leading whitespace on existing lines. They do not split a minified single-line document into a nested layout, so they cannot replace a JSON-aware formatter.
Will formatted JSON stay usable after copying or sharing?
Formatted JSON is still plain text. Copying from Sublime Text copies ordinary characters, and any compliant JSON parser accepts the pretty-printed and compact forms equally. The data does not depend on any special rendering.
The layout is less reliable than the data. Some destinations collapse whitespace, wrap long lines or convert tabs, which can flatten the indentation. The JSON stays valid but becomes harder to read. Chat tools, form fields and rich-text editors are common places where this happens. This review did not test specific destinations, so check the target before relying on the layout. For snippets you share, a code block or a file attachment usually keeps whitespace better than inline text.
Formatting makes the content larger because of the added whitespace and newlines. For storage or transmission where size matters, minify the content or let the producing system serialize it compactly. Formatting is mainly for people reading the content.
Formatting does not change reading order, because tokens stay in sequence unless key sorting is enabled. Search is affected in one respect: a search for a compact pattern such as a key, colon and value with no space will not match after the formatter inserts a space. Use a regular expression that allows optional whitespace, or search on the key alone.
For accessibility, no documentation or scoped tests were available on how screen readers present indented JSON in Sublime Text or elsewhere. It is reasonable to expect a line-per-value layout to be easier to navigate line by line than one very long line, but that is untested here. If assistive technology users will read the output, test with their actual tools.
Bottom line
Pretty-printing JSON in Sublime Text is a sound choice when people need to read and edit the data. It changes only insignificant whitespace in the simple case, and it also catches syntax errors. Before formatting files where it matters, check the formatter's settings for key sorting, non-ASCII escaping and number handling. Keep the original compact or byte-exact text when the content is signed, compared exactly, contains comments, or must stay compact for transport.