JSON arrives in two shapes: a single line of text that nobody can read, or neatly indented text that takes up a screen. Both are valid, and the same data. Switching between them is routine, but there is a third switch in the JSON Formatter that people overlook: sorting the keys. This guide explains what each option changes, and which to choose for the job at hand.
The three operations
| Operation | What changes | What stays the same |
|---|---|---|
| Format | Adds line breaks and indentation | Data, key order, values |
| Minify | Removes every unnecessary space and line break | Data, key order, values |
| Sort keys | Orders the keys of every object alphabetically, at every level | Data and values; order of array items is not changed |
Format and minify are reversible: you can format a minified file and get the same data back. Sort keys changes the order in which keys are written, which does not alter the meaning for objects but may matter for code that depends on order.
Format: for reading
Use format when a person needs to read the JSON: debugging an API response, writing documentation, reviewing a config file. The tool offers an indent of 2 spaces, 4 spaces, 8 spaces or Tab. Two spaces is common in web projects, four in other languages. Use whatever your project's style says, so that files you paste back do not create noise in version control.
Minify: for sending and storing
Minified JSON is smaller, which matters when you send it over a network or store many copies. The output panel shows the change, for example a percentage smaller. A tiny saving matters little; for big payloads it adds up. If your server compresses responses, minifying makes a smaller difference, because compression already removes most whitespace redundancy. Minify when a field must be a single line, such as a JSON value in an environment variable, a log line or a form field.
Sort keys: for comparing
Two JSON files can describe the same data with keys in a different order. A text comparison then shows many changes where nothing actually changed. Sorting the keys of both gives a stable order, so the differences you see are real ones. The workflow:
- Paste the first JSON, switch on Sort keys, press format, and copy the result.
- Do the same with the second.
- Compare the two outputs in the Text Diff Checker.
Sorting also helps when you want a config file in a predictable order for version control, so that new keys always appear in the same place. Do not sort files when the order has meaning for a person, such as a hand-arranged config with the most important settings on top. Arrays are never reordered, because order in an array is data.
Step by step
- Paste your JSON into the JSON input box. It can be minified or messy.
- Choose the Indent size and decide on Sort keys.
- Press Format or Minify.
- If the JSON is valid you see a Valid JSON badge and the output. If not, the box shows the error with the line and column; see the guide to fixing Invalid JSON errors.
- Copy the output.
What formatting cannot do
- Repair broken JSON. The tool is strict: it follows the JSON standard, so comments, single quotes, trailing commas and unquoted keys are errors. It tells you where; you fix them.
- Preserve big numbers exactly. JSON text is parsed by the browser, whose numbers are standard floating-point values. A very long integer ID, with more digits than a computer number can hold exactly, may come out changed after formatting. Keep such IDs as strings in the source.
- Keep duplicate keys. If an object has the same key twice, the later value wins after the round trip.
- Handle huge files. Files over about 10 MB may slow the browser. For those, use a command-line tool.
Choosing at a glance
- Reading an API response: format, 2 spaces.
- Committing a config file: format with the project's indent; sort keys only if the project does.
- Comparing two versions: sort keys on both, then diff.
- Putting JSON into a URL, environment variable or one-line log: minify. For a URL, then percent-encode with the URL Encoder and Decoder.
- Turning records into a spreadsheet: use the JSON to CSV Converter.
Working with the output
After formatting, the output box is read-only and has a copy button; edit the input and format again rather than editing the output. The line and character counts above each box help you check that nothing was lost: formatting changes the number of lines and characters but never the data. If the result needs to go into a file, paste it into your editor and save it with the .json extension, using UTF-8 encoding. When you send minified JSON in an HTTP request, remember to set the content type to application/json. When you paste formatted JSON into a command line, quote it carefully, because the line breaks and double quotes are interpreted by the shell; minifying first often makes that easier.
Privacy of what you paste
API responses often contain personal data, tokens or internal addresses. The formatting runs in your browser and nothing is sent to a server, but the text is still in your clipboard and on your screen. Remove secrets before sharing a formatted response in a ticket or a chat, and avoid pasting production tokens or customer records into any tool when a sample would do.
Frequently asked questions
Is my JSON uploaded?
No. Formatting happens in your browser.
Does formatting change the data?
No. Only whitespace changes, and key order if you chose to sort.
Why is my output a different size than I expected?
Indentation adds characters. The panel shows whether the output is larger or smaller than the input.