Tutorials

How to Format JSON in Cursor

DevUtilHub Team
5 min read
Code editor with a neatly indented JSON document and a cursor beside it

Quick answer: press Shift + Alt + F on Windows and Linux, or Shift + Option + F on macOS. Cursor is built on VS Code, so it uses the same JSON formatter and the same shortcut. You donโ€™t need an extension or a prompt.

If the shortcut does nothing, check the language mode in the bottom-right corner of the status bar. It has to say JSON (or JSON with Comments). A file pasted into an untitled tab often starts as Plain Text.

Need a one-off format without opening an editor? Paste into the JSON Formatter. It validates as well, in your browser.

Everything from VS Code carries over

Cursor reads the same kind of settings.json as VS Code and runs the same JSON language features, so the setup from How to Format JSON in VS Code works unchanged:

  • Command Palette: Ctrl + Shift + P (Cmd + Shift + P on Mac), then โ€œFormat Documentโ€.
  • Format on save: open Settings with Ctrl + , (Cmd + ,) and turn on Editor: Format On Save.
  • Prettier: install the Prettier extension from the Extensions view and set it as the default formatter. See Prettier vs VS Codeโ€™s Built-in JSON Formatter for the trade-offs.

A minimal settings.json that formats both plain JSON and JSON-with-comments files (tsconfig.json, .vscode/settings.json) on save:

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

Open it from the Command Palette with โ€œPreferences: Open User Settings (JSON)โ€. Cursorโ€™s own AI settings live in a separate panel and donโ€™t affect formatting.

The one shortcut thatโ€™s different: Format Selection

In VS Code, Ctrl + K then Ctrl + F (Cmd + K, Cmd + F on Mac) formats only the selected text. In Cursor, Ctrl/Cmd + K opens the inline AI edit box, so that chord doesnโ€™t reach the formatter.

Two ways around it:

  1. Select the text and run Format Selection from the Command Palette.
  2. Open Keyboard Shortcuts (Ctrl + K, Ctrl + S in VS Code; search โ€œKeyboard Shortcutsโ€ in Cursorโ€™s Command Palette), search for โ€œFormat Selectionโ€, and bind it to a key you like. Cursor may have already moved VS Codeโ€™s Cmd + K chords to another prefix, and this is where youโ€™d see it.

Should you ask the AI to format JSON?

For a short snippet, itโ€™s harmless. For a real file, use the formatter:

  • The formatter canโ€™t change your data. It parses the JSON and prints it back. A language model rewrites the text, and in a long file it can drop an array item, round a number, โ€œfixโ€ a value it thinks is a typo, or cut the output short.
  • A model is slower and costs tokens. A 2,000-line config is a lot of output for something Shift + Alt + F does instantly.
  • A model hides errors. If the JSON is invalid, the formatter refuses and the editor underlines the problem. A model will usually just return something valid-looking, and you wonโ€™t know what it changed.

Where the AI is useful: explaining a structure you donโ€™t recognize, writing a JSON Schema for a file, or generating sample data. If it does edit a JSON file, compare the before and after with JSON Diff. It ignores key order, so any change it shows is a real change to the data.

Troubleshooting

โ€œThere is no formatter for โ€˜jsonโ€™ files installed.โ€ The built-in JSON language features extension is disabled. Open the Extensions view, show built-in extensions, and enable โ€œJSON Language Featuresโ€.

Format on save works in data.json but not in tsconfig.json. That file is JSON with Comments. Add the [jsonc] block shown above.

Nothing happens and thereโ€™s no error. The JSON probably has a syntax error, such as a trailing comma or single quotes. The Problems panel (Ctrl + Shift + M) lists them. Debug Malformed JSON covers the usual suspects.

Prettier and the built-in formatter keep fighting. Set editor.defaultFormatter per language, as above, so only one of them runs.

Tags

#json #cursor #vscode #formatting #ai-tools

Share this article

Related Articles