Home / Blog

How to Fix Invalid JSON Errors

If your API call fails with a JSON parse error, this guide gives you a repeatable way to diagnose and fix it. We cover common error patterns, language-specific exceptions, and practical validation workflows used in production teams.

What an Invalid JSON Error Actually Means

An invalid JSON error means the payload is not compliant with strict JSON syntax. Typical parser messages includeUnexpected token, Unexpected end of JSON input, and Expecting property name enclosed in double quotes. JSON parsers are strict by design: a single trailing comma or unescaped quote can break the entire payload.

Start with validation first, then formatting. Use JSON Validator to get line/column errors and JSON Formatter to make the structure readable.

Most Common JSON Parse Error Patterns

Unexpected token JSON

{"name": "Ava", age: 30}

Problem: key age is unquoted.

Trailing comma

{
  "name": "Ava",
  "age": 30,
}

Problem: trailing comma after last field.

Single quotes or unescaped content

{'name':'Ava'}

Problem: JSON requires double quotes for keys and strings.

Unexpected token < in JSON

This usually means your endpoint returned HTML (often an error page or auth redirect) while your code expected JSON. Inspect the raw response body before parsing.

5-Minute Troubleshooting Workflow

  1. Copy raw payload (before any object mapping).
  2. Validate in JSON Validator.
  3. Fix exact line/column issue.
  4. Reformat in JSON Formatter.
  5. Compare old vs fixed payload in JSON Diff.
Error messageLikely causeFix
Unexpected token }Trailing commaRemove final comma
Unexpected token <HTML response, not JSONInspect response body and content type
Expecting property nameUnquoted keyUse double-quoted keys

Language-Specific Examples

JavaScript

try {
  const data = JSON.parse(raw);
} catch (err) {
  console.error('JSON parse error:', err.message);
}

Python

import json

try:
    data = json.loads(raw)
except json.JSONDecodeError as e:
    print(e.msg, e.lineno, e.colno)

C#

using System.Text.Json;

try {
  var doc = JsonDocument.Parse(raw);
} catch (JsonException ex) {
  Console.WriteLine(ex.Message);
}

Java

ObjectMapper mapper = new ObjectMapper();
try {
  Object value = mapper.readValue(raw, Object.class);
} catch (Exception e) {
  System.out.println(e.getMessage());
}

Real Use Cases from Developer Teams

  • API integration debugging: broken payloads between staging and production.
  • Config deployments: malformed JSON in env-specific config files.
  • ETL pipelines: partial records containing invalid escape characters.
  • Webhook processing: unexpected HTML or text response from upstream systems.

Screenshot Recommendations

  • Validator view showing line-numbered parse error.
  • Before/after formatted payload screenshot.
  • JSON Diff screenshot comparing failing and fixed payload.

Pro Tips

  • Always log raw responses when parse fails.
  • Validate first, map to domain models second.
  • Treat "Unexpected token <" as a response-type bug, not JSON syntax bug.
  • Use schema checks after syntax validation for robust pipelines.

FAQ

How do I fix invalid JSON quickly?

Paste into a validator, fix the exact line/column error, then re-run parse.

Why does JSON.parse fail while payload looks correct?

Often hidden issues: trailing commas, bad escaping, or non-JSON response content.

Can I validate large JSON online?

Yes. Use JSON Validator and then JSON Formatter for readable inspection.

Get Started with formatterjson.org

Validate malformed payloads with JSON Validator, format for readability with JSON Formatter, and compare payload versions with JSON Diff.