DEV Community

Cover image for 6 Common JSON Errors That Break APIs (And How to Fix Them)
BuildLab
BuildLab

Posted on

6 Common JSON Errors That Break APIs (And How to Fix Them)

JSON is used everywhere in web development, especially when working with APIs, configuration files, and data exchange.

But even a small syntax mistake can cause parsing to fail. Sometimes the problem is obvious; other times, it is buried inside a large, nested response.

Here are six common JSON errors, why they happen, and how to fix them.

1. Trailing commas

A trailing comma is easy to miss when writing an object or array.

Invalid JSON

{
  "name": "Alice",
  "role": "developer",
}
Enter fullscreen mode Exit fullscreen mode

The comma after "developer" makes this invalid JSON.

Correct JSON

{
  "name": "Alice",
  "role": "developer"
}
Enter fullscreen mode Exit fullscreen mode

JavaScript allows trailing commas in many situations, but standard JSON does not.

2. Using single quotes

JSON requires double quotes around strings and object keys.

Invalid JSON

{
  'name': 'Alice'
}
Enter fullscreen mode Exit fullscreen mode

Correct JSON

{
  "name": "Alice"
}
Enter fullscreen mode Exit fullscreen mode

This often happens when copying a JavaScript object and assuming it is valid JSON.

Remember: a JavaScript object and a JSON document are not the same thing.

3. Forgetting quotation marks around keys

Object keys must be double-quoted strings.

Invalid JSON

{
  name: "Alice",
  age: 25
}
Enter fullscreen mode Exit fullscreen mode

Correct JSON

{
  "name": "Alice",
  "age": 25
}
Enter fullscreen mode Exit fullscreen mode

When an API rejects a payload that looks correct at first glance, check whether every key is enclosed in double quotes.

4. Using unsupported values

JavaScript supports values such as undefined, NaN, and Infinity, but JSON does not.

Invalid JSON

{
  "username": undefined,
  "score": NaN
}
Enter fullscreen mode Exit fullscreen mode

A valid alternative might be:

{
  "username": null,
  "score": 0
}
Enter fullscreen mode Exit fullscreen mode

Choose replacement values according to what the data actually means. null and 0 are not interchangeable.

JSON also doesn't support comments.

5. Incorrectly escaping quotation marks

Strings containing quotation marks need proper escaping.

Invalid JSON

{
  "message": "She said "hello""
}
Enter fullscreen mode Exit fullscreen mode

The parser encounters a quotation mark before hello and treats it as the end of the string.

Correct JSON

{
  "message": "She said \"hello\""
}
Enter fullscreen mode Exit fullscreen mode

The same principle applies to other special characters. For example, use \n to represent a line break inside a JSON string.

6. Missing commas or mismatched brackets

This becomes particularly frustrating with deeply nested objects.

Invalid JSON

{
  "user": {
    "name": "Alice"
    "role": "admin"
  }
}
Enter fullscreen mode Exit fullscreen mode

There is no comma between the two properties.

Correct JSON

{
  "user": {
    "name": "Alice",
    "role": "admin"
  }
}
Enter fullscreen mode Exit fullscreen mode

When debugging a large JSON document, inspect the area around the reported error. The actual mistake may occur earlier than the position shown by the parser.

How to validate JSON in JavaScript

You can use the built-in JSON.parse() method to check whether a string contains valid JSON.

const rawJson = `{
  "user": {
    "name": "Alice",
    "role": "developer"
  }
}`;

try {
  const data = JSON.parse(rawJson);
  console.log("Valid JSON:", data);
} catch (error) {
  console.error("Invalid JSON:", error.message);
}
Enter fullscreen mode Exit fullscreen mode

If the input is valid, JSON.parse() returns the corresponding JavaScript value. Otherwise, it throws a syntax error.

The exact error message depends on the JavaScript runtime, so avoid relying on a particular message when writing error-handling code.

My JSON debugging workflow

When I need to investigate malformed JSON, this is the process I recommend:

  1. Validate the original input.
  2. Inspect the reported error and nearby characters.
  3. Check quotation marks, commas, braces, brackets, and escape sequences.
  4. Fix the syntax and validate it again.
  5. Format the valid JSON to make nested data easier to inspect.

Automatic repair tools can save time, but they should be used carefully. Producing valid JSON is not enough if a repair silently changes the meaning of the original data.

A tool for working with JSON

I built Instant JSON, a browser-based toolkit for formatting, validating, minifying, repairing, sorting, and comparing JSON.

The goal is to make everyday JSON tasks easier to handle in one place, with processing performed directly in the browser rather than uploading JSON payloads to a server.

I'm continuing to improve it and would welcome constructive feedback from developers who work with JSON regularly.

What's the most common JSON error you encounter when debugging APIs, and what is your go-to way to find it?

Top comments (1)

Collapse
 
buildlabs profile image
BuildLab •

Drop a comment about what do you think!!!