keepcalculator

06 Numbers and Powers

Fix JSON Parsing Errors from Comments and Trailing Commas

Card No. 06·27074 · Updated · Standard formula, shown below

This guide shows you how to fix JSON parsing errors caused by comments or trailing commas, two of the most common mistakes when working with JSON files or API responses. You’ll learn how to identify these errors, correct them manually, and use tools to automate the cleanup.

What You’ll Need

  • A text editor (like VS Code, Sublime Text, or Notepad++)
  • Access to a JSON parser or validator (online or in your development environment)
  • Basic understanding of JSON syntax (objects, arrays, key-value pairs)

Step-by-Step

  1. Reproduce the error. Open the file or paste the JSON string into your parser. Note the exact error message and the line/column number if provided. This tells you where the parser got confused.
  2. Look for comments. JSON does not allow comments. Search for lines starting with // or blocks between /* */. If you find any, remove them entirely. If you need to keep notes, move them to a separate _comment field in the JSON object (e.g., "_comment": "This is a note").
  3. Find trailing commas. Trailing commas are commas after the last item in an object or array. For example, {"a": 1,} or [1, 2,]. These are invalid in strict JSON. Remove the comma that follows the last key-value pair or element.
  4. Check for nested structures. Comments and trailing commas can appear inside nested objects or arrays. Use your editor’s search to find all commas, then verify each one is between two items, not after the last.
  5. Validate your fix. After removing comments and trailing commas, run the JSON through a validator again. It should parse without errors. If you’re using a code editor, many have built-in JSON validation (e.g., VS Code shows a red squiggly line).
  6. Use a linter or formatter. If you’re dealing with a large file, use a tool like Prettier or a JSON linter to automatically strip comments and fix trailing commas. For example, Prettier can be configured to remove trailing commas in JSON files. Alternatively, you can use an online JSON cleaner.
  7. Prevent future issues. If you’re writing JSON by hand, avoid adding comments. If you need to document, use a separate documentation file. For trailing commas, get into the habit of double-checking the last item in each object/array. Many code editors can be configured to flag trailing commas as errors.

Common Mistakes

  • Confusing JSON with JavaScript objects. JavaScript allows comments and trailing commas, but JSON does not. This is the most common source of the error.
  • Removing the wrong comma. When you see a trailing comma, make sure you remove the comma that is after the last item, not the comma that separates two items. For example, in {"a": 1, "b": 2,}, remove the comma after 2, not the one after 1.
  • Missing nested cases. A trailing comma might be in a nested array or object. Always check the entire structure, not just the top level.
  • Accidentally deleting a colon or quote. When editing, be careful not to remove a colon or a closing quote. That will introduce a new error.

Tips

  • Use a JSON-aware editor. Editors like VS Code highlight syntax errors in real time, making it easier to spot issues.
  • Enable “JSON with Commentsâ€� mode if you must keep comments. Some tools (like VS Code) support a JSONC format that allows comments. But remember, this is not standard JSON and will not work with all parsers.
  • Format your JSON before debugging. Use a formatter to make the structure clear. This helps you see where a comma is out of place.
  • Use a linter in your build process. Add a JSON linter to your CI/CD pipeline to catch these errors before they reach production.

Troubleshooting

If you’ve removed comments and trailing commas but still get an error:

  • Check for invisible characters. Sometimes a non-breaking space or other Unicode character sneaks in. Use a hex editor or a tool that shows all characters to inspect the file.
  • Verify the file encoding. Ensure the file is saved as UTF-8. Other encodings can cause parsing issues.
  • Look for unescaped quotes. If a string contains a double quote, it must be escaped with a backslash. For example, "He said \"hello\"".
  • Check for single quotes. JSON requires double quotes for strings. Single quotes are not allowed.
  • Test with a minimal example. Isolate the problematic part by creating a small JSON snippet and testing it. This helps narrow down the issue.

Frequently Asked Questions

Can I use comments in JSON?

No, standard JSON does not support comments. If you need comments, use a JSONC file (if your tool supports it) or move notes to a separate field.

Why are trailing commas invalid in JSON?

The JSON specification (RFC 8259) explicitly disallows trailing commas. They were left out to keep the format simple and to avoid ambiguity in some parsers.

How do I remove comments from a JSON file automatically?

You can use a tool like json5 (a JSON superset that allows comments) to parse the file, then serialize it back to strict JSON without comments. Or use a regex-based cleanup script, but be careful with strings that contain //.

What is the difference between JSON and JavaScript objects?

JavaScript objects are a data structure in the language, and they allow comments and trailing commas. JSON is a text format derived from JavaScript but with stricter rules.

Can I use a linter to catch these errors?

Yes, many linters (like ESLint with the json plugin) can validate JSON files and flag comments and trailing commas as errors.

Why does my parser say “Unexpected token � at the end of the file?

This often happens when there is a trailing comma after the last item, or when there is a stray comma or brace. Check the end of your JSON structure carefully.

Related

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

More cards in this drawer

The whole Numbers and Powers drawer →