Skip to content

SyntaxError: Unexpected token in JSON

Error message

                Unexpected token X in JSON at position N
              

Quick summary

You called JSON.parse() on a string that does not conform to JSON syntax — JSON requires double-quoted keys and string values, no trailing commas, no comments.

Why this happens

  • The JSON string has single quotes instead of double quotes

  • There is a trailing comma in an array or object

  • The string contains JavaScript syntax that is not valid JSON (functions, undefined, comments)

  • The string is empty or not a JSON structure at all

Minimal example

✗ Broken code
const obj = JSON.parse("{'name': 'Ada'}");
✓ Fixed code
const obj = JSON.parse('{"name": "Ada"}');

JSON requires double quotes for keys and string values. Single quotes cause a parse error.

How to diagnose

  • Log the string being parsed to inspect its format

  • Use a JSON validator to find the syntax error

  • Check for common issues: single quotes, trailing commas, comments

How to fix

  • Fix the JSON string to use proper JSON syntax (double quotes, no trailing commas)

  • Use a JSON formatter/validator to identify the exact issue

  • Wrap JSON.parse in try/catch to handle invalid input gracefully

How to prevent

  • Always use JSON.stringify() to produce JSON, not manual string building

  • Validate API responses before parsing

Related Resources

Related Glossary

Related Lessons

Related Tools

← Back to language