Tool Comparisons

Prettier vs VS Code's Built-in JSON Formatter

DevUtilHub Team
6 min read
Two code editor panes showing the same JSON document laid out in two different styles

Short version: VS Codeโ€™s built-in JSON formatter is enough when you work alone and only care about indentation. Use Prettier when a team or CI has to agree on one layout, or when you already use Prettier for JavaScript and CSS. Both ship JSON that parses the same way. They differ in how they lay out short arrays and objects, and in how you enforce the style.

Both are triggered the same way: Shift + Alt + F on Windows and Linux, Shift + Option + F on macOS. Which one runs depends on editor.defaultFormatter. If you havenโ€™t set up either yet, start with How to Format JSON in VS Code.

The same input, two outputs

Take this one-liner:

{"name":"api","ports":[8080,8443],"tls":{"enabled":true}}

The built-in formatter (vscode.json-language-features) puts every property and every array item on its own line:

{
  "name": "api",
  "ports": [
    8080,
    8443
  ],
  "tls": {
    "enabled": true
  }
}

Prettier fits arrays on one line when theyโ€™re under printWidth (80 characters by default). For objects, it keeps the choice you made: if the original had a line break after {, the object stays expanded. Otherwise itโ€™s collapsed when it fits. Formatting the one-liner above gives:

{ "name": "api", "ports": [8080, 8443], "tls": { "enabled": true } }

Add a line break after the first { and run it again, and you get the expanded layout most people expect in a config file:

{
  "name": "api",
  "ports": [8080, 8443],
  "tls": { "enabled": true }
}

Neither output is wrong. The built-in style gives very stable diffs, because adding one array item changes exactly one line. Prettierโ€™s output is shorter and easier to scan, but an array that grows past 80 characters jumps from one line to many, and that shows up as a bigger diff.

How they differ

Built-in (vscode.json-language-features) Prettier (esbenp.prettier-vscode)
Install Ships with VS Code Extension, ideally plus prettier in devDependencies
Indentation editor.tabSize / editor.insertSpaces tabWidth / useTabs in .prettierrc
Short arrays Always expanded (unless json.format.keepLines) Collapsed when they fit in printWidth
package.json Same rules as any JSON file Formatted to match JSON.stringify output, the way npm writes it
Comments (jsonc) Kept Kept
Sorts keys No No
Run in CI No CLI prettier --check .
Config lives in Your editor settings The repository

The last two rows usually decide it. The built-in formatter only exists inside VS Code, so nothing stops a teammate on another editor (or another VS Code setup) from committing a different layout. Prettierโ€™s config is a file in the repo, and prettier --check in CI fails the build when someone skips it.

When the built-in formatter is enough

  • Personal projects, scratch files and API responses you paste in to read.
  • Repos without a JavaScript toolchain, where adding Node just to format JSON would be odd.
  • You want one-line-per-value diffs in data files that change often.

Settings for that setup, including the [jsonc] block that tsconfig.json and settings.json need:

{
  "[json]": {
    "editor.defaultFormatter": "vscode.json-language-features",
    "editor.formatOnSave": true
  },
  "[jsonc]": {
    "editor.defaultFormatter": "vscode.json-language-features",
    "editor.formatOnSave": true
  }
}

When Prettier is worth it

  • More than one person edits the files.
  • You already run Prettier on JS, TS, CSS or Markdown, and JSON should follow the same rules.
  • You want CI to enforce formatting rather than relying on editor settings.

Install it in the project so the editor and CI use the same version:

npm install --save-dev prettier
echo '{ "tabWidth": 2 }' > .prettierrc
npx prettier --write "**/*.json"

Then point VS Code at it for both JSON modes:

{
  "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }
}

Committing those lines in .vscode/settings.json means everyone who opens the repo in VS Code gets the same formatter.

Common mix-ups

โ€œFormat Documentโ€ picks the wrong formatter. With both installed and no default set, VS Code asks which one to use, or uses whichever you chose last time. Set editor.defaultFormatter per language as shown above.

Prettier ignores a file. Check for a .prettierignore entry. Prettier also skips files listed in .gitignore, so generated JSON in dist/ wonโ€™t be touched.

The layout changes after someone else saves. Two people, two formatters. Commit .vscode/settings.json and a .prettierrc, and run prettier --check in CI.

Keys end up in a different order. Neither formatter sorts keys. If a tool you use rewrites the file with sorted keys, it isnโ€™t the formatter. To compare two files where only the key order differs, use JSON Diff, which ignores key order.

Quick checks without either

For a one-off paste, such as an API response or a log line, opening the editor is often slower than pasting into the JSON Formatter. It formats and validates in the browser and uploads nothing. On a server, jq . does the same job; see Format JSON on the Command Line.

Tags

#json #vscode #prettier #formatting #developer-tools

Share this article

Related Articles