Guide

How to Fix JSON Parse Errors

JSON is stricter than JavaScript object syntax. Almost every parse error comes from one of a short list of mistakes.

Updated

Try it now: JSON Validator. It runs in your browser, with no signup.

The ten usual causes

  • Trailing comma after the last item: {"a": 1,}. Remove the comma.
  • Single quotes: {'a': 1}. JSON requires double quotes for keys and strings.
  • Unquoted keys: {a: 1}. Every key must be a double-quoted string.
  • Comments (// or /* */). JSON has no comment syntax. Strip them, or use a format that allows comments for config files.
  • Values that do not exist in JSON: undefined, NaN and Infinity. Use null or a string.
  • Unescaped characters inside strings: a raw line break, a tab or a double quote. Write them as \n, \t and \".
  • A byte order mark (BOM) at the start of a file, which looks like invisible text before the opening brace.
  • Numbers with leading zeros or a leading plus, such as 007 or +5, and hexadecimal literals such as 0xFF.
  • Several JSON documents in one file, as in logs with one object per line. Parse each line separately.
  • A truncated response, where the closing bracket is missing because the download or copy was cut short.

Find the exact position

Error messages differ between browsers and runtimes, but most point to a position or a line and column. Look at the character just before the reported position as well, because the real mistake is often the comma or quote that came earlier.

// JavaScript
try {
  JSON.parse(text);
} catch (e) {
  console.error(e.message);   // wording differs between browsers
}

# Python reports line and column
import json
try:
    json.loads(text)
except json.JSONDecodeError as e:
    print(e.msg, "line", e.lineno, "column", e.colno)

Check it quickly from the terminal

Paste the text into the JSON Validator to see the parser error, then format it with the JSON Formatter so the structure is easy to read.

# Python's built-in tool: exits with an error and a position if invalid
python -m json.tool response.json

# jq prints the parse error
jq . response.json

Prevent it at the source

  • Produce JSON with a serializer, not by joining strings by hand.
  • Set the Content-Type header to application/json and check the response status before parsing.
  • Read the body as text and log it when a parse fails, so you can see what actually arrived.

Frequently asked questions

Why does JSON.parse fail on a JavaScript object literal?

Object literals allow unquoted keys, single quotes and trailing commas. JSON allows none of them.

Can JSON contain comments?

No. Some config tools accept comments in their own variants, but standard JSON parsers, including JSON.parse, reject them.

Is a plain string or number valid JSON?

Yes. Under the current standard a JSON text can be any value, not only an object or array.

Tools used in this guide

onlinedevelopertool.com is a collection of 54 free, browser-based utilities for the small jobs that interrupt real work: formatting a JSON response, decoding a token, converting a timestamp, generating a UUID or checking a hash. Each tool has its own page with a short explanation, a worked example and answers to common questions, so you can use it and learn what it does at the same time.

There is no account to create and nothing to install. Tools run in your browser, so the text you paste is processed on your own device. The one exception is the Website Status Checker, which sends a request from your browser to the address you enter.

Three steps: paste your input, click the tool, copy the result, all inside your browser
Popular developer utilities: JWT decoder, UUID generator, Base64 decoder, SHA-256 hash, regex tester and timestamp converter

What you can do here

  • JSON Tools

    Format, validate, beautify and minify JSON, generate C#, TypeScript, Java, Python, Go, PHP and Kotlin models from it, or convert between JSON, YAML and CSV.

  • Encoders & Decoders

    URL, Base64 and HTML encoding and decoding, plus a JWT decoder for reading token claims.

  • Generators & Hashes

    Generate UUIDs and JWTs, validate UUIDs, and create MD5, SHA-256 and SHA-512 hashes.

  • Web Tools

    Timestamps, color codes, query strings and cron expressions - the small things web work keeps throwing at you.

  • Text & Data

    Format XML and YAML, count text, change case, make slugs, test regex, diff two texts and convert number bases.

  • Website Checking

    Check whether a website or URL is reachable from your browser.

Quick developer reference

  • JSON

    Keys and strings must use double quotes, and trailing commas and comments are not allowed. Those are the three most common reasons a payload fails to parse.

    Validate JSON
  • JWT

    A JSON Web Token is three Base64URL parts joined by dots: header, payload and signature. Decoding shows the claims but does not prove the token is genuine; only signature verification does.

    Decode a JWT
  • Base64

    Base64 is an encoding, not encryption. Anyone can reverse it, and it makes data roughly one third larger, so never use it to hide secrets.

    Decode Base64
  • UUID

    A version 4 UUID is made of random bits and written as 8-4-4-4-12 hexadecimal characters. Collisions are astronomically unlikely, which is why they work well as identifiers.

    Generate a UUID
  • Timestamps

    A Unix timestamp counts time since 1 January 1970 UTC. Ten digits usually means seconds and thirteen digits usually means milliseconds.

    Convert a timestamp
  • Hashes

    MD5 is fine for quick checksums but is not safe for security. Prefer SHA-256 or SHA-512, and remember that hashing is one-way and is not encryption.

    Create a SHA-256 hash

Missing a tool you need? Visit the contact page and tell us what you reach for most often. You can also browse every tool or read the About page to see how the site is built and what it does with your data.