โ† Back to Blog

How to Prettify Minified JSON

2026-04-12 ยท 5 min read

What Is JSON Prettifying?

JSON prettifying is another term for formatting โ€” specifically, converting minified single-line JSON into a readable multi-line format with indentation and line breaks. It's the inverse of JSON minification; both operations are completely reversible โ€” data content is unchanged, only the presentation changes.

Typical scenarios where developers need to prettify JSON: extracting minified JSON from API logs for analysis; reading JSON responses returned by third-party libraries; converting JSON data to readable format for pasting into documentation or sharing with colleagues; reviewing JSON configuration changes in code review.

Fastest Prettification Methods: Tool Comparison

Ordered by speed and convenience:

  1. Browser console (fastest, no window switching): console.log(JSON.parse(jsonStr)), directly shows interactive tree JSON in the page console
  2. Online tool: Paste-click-copy, the entire process takes ~5 seconds. Good for occasional use
  3. VS Code: Create a new .json file, paste content, press Shift+Alt+F, ~10 seconds total
  4. Command-line jq: echo 'minifiedJSON' | jq ., good in terminal workflows
  5. Python command: echo 'minifiedJSON' | python3 -m json.tool

Prettification Code in Each Language

// JavaScript
const pretty = JSON.stringify(JSON.parse(minified), null, 2);

# Python
import json
pretty = json.dumps(json.loads(minified), indent=2, ensure_ascii=False)

// Go
var obj interface{}
json.Unmarshal([]byte(minified), &obj)
pretty, _ := json.MarshalIndent(obj, "", "  ")

// Java (Jackson)
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(minified, Object.class);
String pretty = mapper.writerWithDefaultPrettyPrinter()
                      .writeValueAsString(json);

Performance Optimization for Large JSON Prettification

For very large JSON files (hundreds of MB or even GB-scale), the standard approach of "fully parse first, then re-serialize" consumes large amounts of memory. A more efficient method is streaming processing โ€” reading JSON in chunks and outputting prettified results without loading everything into memory at once.

The command-line tool jq supports streaming mode (--stream), capable of handling JSON files of any size without exhausting memory. For JSON files over 1GB, this is the only viable local processing approach.

Sorting Keys in Prettified JSON

By default, prettifying doesn't change the order of object keys (preserves the original order). But sometimes sorting key names alphabetically makes it easier to compare differences between two JSON objects:

// JavaScript - ๆŒ‰้”ฎๅๆŽ’ๅบ็พŽๅŒ–
function sortAndPrettify(obj) {
  return JSON.stringify(
    obj,
    (key, value) => {
      if (value && typeof value === 'object' && !Array.isArray(value)) {
        return Object.fromEntries(
          Object.entries(value).sort(([a], [b]) => a.localeCompare(b))
        );
      }
      return value;
    },
    2
  );
}

# Python - ๆŒ‰้”ฎๅๆŽ’ๅบ็พŽๅŒ–
json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False)

# jq - ๆŒ‰้”ฎๅๆŽ’ๅบ
jq -S . data.json  # -S ่กจ็คบ sort keys

Prettification for Code Review

In Git version control, if JSON configuration files are stored in minified format, git diff after any change shows only a single line of change โ€” completely obscuring what was actually modified. Storing JSON configuration files in formatted (prettified) state lets git diff clearly show specific field changes, dramatically improving code review efficiency and reliability.

Team conventions recommendation: all JSON files in version control should be stored in formatted (prettified) state (using consistent indentation, 2 spaces recommended); production API responses and deployed static JSON files can use minified format; don't mix both formats in the same file.

Prettier and Code Formatting Tools

In frontend development projects, prettier is the standard code formatting tool, and it also supports automatic JSON file formatting. Adding a .prettierrc config to the project root standardizes JSON formatting across the team:

// .prettierrc
{
  "tabWidth": 2,
  "printWidth": 80,
  "trailingComma": "es5"
}

Combined with a Git pre-commit hook (like lint-staged), you can automatically format all JSON files on every commit, ensuring the repository never contains inconsistently formatted JSON and reducing unnecessary formatting noise in code reviews.

Try the free tool now

Use Free Tool โ†’