ThisCalculator

Everyday

VS Code Invalid JSON: Why It Happens & How to Fix

Updated · Standard formula, shown below

VS Code does not intentionally let you write invalid JSON; its JSON language server is designed to flag syntax errors, but it only validates files it recognizes as JSON or JSONC, and it does not block you from saving or running invalid JSON. The confusion comes from how VS Code handles file extensions, comments, and trailing commas, which can make invalid JSON look valid or vice versa.

Background: How VS Code Decides What Is JSON

VS Code uses a language server called vscode-json-languageservice to provide IntelliSense, validation, and formatting for JSON files. The server only activates for files it recognizes as JSON. By default, that means files with the .json extension, but also .jsonc (JSON with Comments), .code-snippets, and certain other files like tsconfig.json or package.json that are associated with the JSON language mode.

If a file has a different extension (e.g., .txt or .log) but contains JSON, VS Code will not validate it. Similarly, if you open a file with a .json extension but write JavaScript-like syntax (e.g., single quotes, unquoted keys, comments), the language server will show errors—but it won’t stop you from saving. The editor is a text editor first; it does not enforce correctness unless you have a formatter or linter configured to do so on save.

Why Invalid JSON Can Appear Valid

There are several common reasons why you might think VS Code is letting you write invalid JSON without complaint:

  • Comments and trailing commas: VS Code supports JSONC (JSON with Comments) in many default files like settings.json and launch.json. In JSONC, comments (// and /* */) and trailing commas are allowed. If you copy that syntax into a strict .json file, the editor will flag it—but if the file is recognized as JSONC, it won’t.
  • File extension mismatch: A file named data.txt containing {'a': 1} will not be validated at all. You can write anything and VS Code will not complain.
  • Language mode override: You can manually set the language mode (bottom-right corner of the window) to JSON or JSONC. If a file is set to Plain Text, no validation occurs.
  • Errors are subtle: The squiggly underline for JSON errors is often faint, especially on a light theme. A missing comma or a stray quote may be highlighted with a small red squiggle that is easy to miss.

How to Check If Your JSON Is Actually Valid

If you suspect your file is invalid but VS Code isn’t showing errors, do the following:

  1. Look at the bottom-right corner of the window. It shows the language mode (e.g., JSON, JSONC, Plain Text). Click it and select JSON if it isn’t already.
  2. Run the command Format Document (Shift+Alt+F on Windows/Linux, Shift+Option+F on macOS). If the file is valid JSON, it will be formatted. If it’s invalid, you’ll see an error message like “Unable to format” or a popup explaining the syntax error.
  3. Use the built-in JSON parser: open the Command Palette (Ctrl+Shift+P) and run Developer: Show Running Extensions to confirm the JSON language server is active. Then open a .json file and look for any yellow or red squiggles.
  4. Copy the content into an online JSON validator (like jsonlint.com) or use a Node.js command: node -e "JSON.parse(require('fs').readFileSync('file.json'))" in the terminal.

Common Misconceptions About VS Code and JSON

One widespread misconception is that VS Code automatically validates JSON on save and refuses to save invalid files. That is false. VS Code is not a compiler; it does not block saving. Validation is purely visual—it shows squiggles and error messages in the Problems panel (Ctrl+Shift+M), but you can save and even run an invalid JSON file if your runtime doesn’t parse it.

Another misconception is that JSONC is a standard. It is not; it is a VS Code-specific extension of JSON that allows comments and trailing commas. Other tools (like Node.js, Python’s json module, or web browsers) do not support JSONC. If you copy a JSONC file to a strict JSON context, it will fail.

Also, some users think that the json.schemas setting in VS Code can make validation stricter. That setting is for schema validation (checking structure against a defined schema), not for basic syntax. Even with a schema, syntax errors are still checked by the language server.

How to Enable Strict Validation

If you want VS Code to be stricter about JSON, you can:

  • Set the language mode to JSON (not JSONC) for files that must be strict. This will flag comments and trailing commas.
  • Install an extension like Prettier or ESLint (with a JSON plugin) and configure it to format on save. Prettier, for example, will remove trailing commas and comments when formatting a .json file, effectively fixing invalid syntax.
  • Use a task or build script that runs a JSON parser before deployment. For example, add a pre-commit hook that runs node -e "JSON.parse(...)".
  • Enable the Problems panel (Ctrl+Shift+M) and make sure the JSON language server is not disabled. You can check by running Developer: Show Running Extensions.

Comparison: JSON vs JSONC vs JavaScript Objects

It helps to understand the differences:

  • JSON is a strict data-interchange format. No comments, no trailing commas, keys must be double-quoted, strings must be double-quoted, and numbers cannot have leading zeros.
  • JSONC (JSON with Comments) is a superset that allows // and /* */ comments and trailing commas. VS Code uses it for configuration files like settings.json and tasks.json.
  • JavaScript object literals are even looser: single quotes, unquoted keys (if valid identifiers), functions, and expressions. A JavaScript object is not JSON unless it follows the strict rules.

When you write {name: "John"} in a .js file, that’s a JavaScript object, not JSON. If you put that in a .json file, VS Code will show an error because the key name is not double-quoted.

Frequently Asked Questions

Why does VS Code allow comments in JSON files?

VS Code treats many of its own configuration files (like settings.json, keybindings.json, and launch.json) as JSONC, which allows comments. This is intentional because those files are meant to be edited by humans, and comments help document settings. However, if you use a .json file for data exchange (e.g., an API response), comments will be flagged as errors.

How do I make VS Code show errors for invalid JSON?

Ensure the file has a .json extension and the language mode is set to JSON (not JSONC or Plain Text). Then look at the Problems panel (Ctrl+Shift+M). If you still don’t see errors, try closing and reopening the file, or restarting VS Code.

Can I configure VS Code to automatically fix invalid JSON on save?

Yes, you can install the Prettier extension and set "editor.formatOnSave": true in your user settings. Prettier will reformat the file, which often fixes missing commas, indentation, and other syntax issues. However, Prettier cannot fix a missing closing brace or an unquoted key; it will show an error instead.

Why does my JSON work in VS Code but not in my browser?

Most likely because you are using JSONC features (comments or trailing commas) that VS Code tolerates but browsers do not. Browsers use the strict JSON.parse() function, which throws an error on comments or trailing commas. Remove them and the file will work.

Is there a way to validate JSON without installing anything?

Yes, use the built-in command Format Document. If the file is valid, it will be formatted. If not, you’ll get an error. You can also open the Command Palette and run JSON: Open JSON Schema to see if a schema is applied, but that doesn’t validate syntax.

What is the difference between a JSON error and a schema error in VS Code?

A JSON error is a syntax error (e.g., missing comma, unquoted key). A schema error is a structural error (e.g., a required property is missing, or a property has the wrong type). VS Code shows both in the Problems panel, but schema errors only appear if a schema is associated with the file (via json.schemas or a $schema property).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.