AllOneTools Logo
AllOneTools

Editors guide · 4 min read

How to format JSON in VS Code

Four ways to pretty-print JSON in VS Code — built-in command, Prettier, keyboard shortcut, and CLI. Plus fixes for 'format document' not working.

Last updated:

[object Object]

[object Object]

1 Method 1 — Built-in Format Document

[object Object]

    [object Object]

[object Object]

2 Method 2 — Keyboard shortcut

[object Object]

    [object Object]

[object Object]

3 Method 3 — Format on Save

[object Object]

    [object Object]

[object Object]

[object Object]

4 Method 4 — Prettier (recommended for teams)

[object Object]

[object Object]

[object Object]

[object Object]

[object Object]

5 Fix: 'Format Document' does nothing

[object Object]

    [object Object]

Frequently asked questions

Can I format JSON with 4-space indentation?

Yes. Open settings, search for "tab size", and set **Editor: Tab Size** to 4. Or add `"editor.tabSize": 4` to your settings.json for JSON files specifically.

How do I format JSON with comments (JSONC)?

VS Code treats `.jsonc` files specially — same formatter, comments preserved. Change the file's language mode (bottom-right corner) to `JSON with Comments` if auto-detection fails.

Does this work for JSON embedded in other files (HTML, Markdown)?

Partially. The built-in formatter only formats whole-file JSON. For embedded JSON, select the JSON block and run "Format Selection" — if your embedded language grammar supports it, it'll work.

What about formatting JSON from the command line?

Use `jq . input.json` or `python -m json.tool input.json`. Both are installed on most dev machines already.

Keep exploring