Skip to content

JSON Parse Failure on Malformed String

Quick summary

You passed a string that is not valid JSON to JSON.parse — even a single trailing comma or unquoted key makes it fail.

Why this happens

  • Trailing comma in an array or object (valid in JS but not in JSON)

  • Single-quoted strings or unquoted property keys

  • Comments or undefined/NaN values that JSON does not support

Minimal example

✗ Broken code
const data = JSON.parse('{"name": "Ada", }'); // SyntaxError: Unexpected token }
✓ Fixed code
const data = JSON.parse('{"name": "Ada"}'); // valid
console.log(data.name); // "Ada"

The trailing comma after "Ada" is illegal in JSON. Removing it produces valid JSON that JSON.parse can read into an object.

How to diagnose

  • Read the SyntaxError message — it names the character position of the failure

  • Print the raw string and inspect it around the reported position

  • Validate the string with a JSON linter before parsing

How to fix

  • Remove trailing commas, use double quotes, and quote all property keys

  • Strip comments and replace undefined/NaN with null before serializing

  • Wrap JSON.parse in try/catch and fall back to a safe default on failure

How to prevent

  • Always produce JSON with JSON.stringify rather than hand-building strings

  • Validate external input with a schema validator before parsing

Related Resources

Related Glossary

Related Lessons

  • Strings

    Learn about JSON and string parsing.

← Back to language