DEV Community

ByteKit
ByteKit

Posted on

A Practical JSON Debugging Checklist: 4 Common Errors and How to Fix Them

A Practical JSON Debugging Checklist: 4 Common Errors and How to Fix Them

JSON is easy to read and write, but a tiny syntax mistake can make an entire API response or configuration file impossible to parse.

When JSON.parse() throws an error, the problem is often one of a few common mistakes.

Here are four issues worth checking before spending time debugging your application.

1. Trailing Commas

JSON does not allow a comma after the last property in an object or the last element in an array.

Incorrect:

{
  "name": "Alex",
  "role": "Developer",
}
Enter fullscreen mode Exit fullscreen mode

Correct:

{
  "name": "Alex",
  "role": "Developer"
}
Enter fullscreen mode Exit fullscreen mode

This mistake is especially common when copying JavaScript objects into a JSON file. JavaScript object literals may allow trailing commas, but standard JSON does not.

2. Single Quotes Instead of Double Quotes

JSON requires double quotes around property names and string values.

Incorrect:

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

Correct:

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

If you're manually creating JSON, check your quotation marks first.

3. Missing Commas Between Properties

Each property in an object must be separated by a comma.

Incorrect:

{
  "name": "Alex"
  "age": 28
}
Enter fullscreen mode Exit fullscreen mode

Correct:

{
  "name": "Alex",
  "age": 28
}
Enter fullscreen mode Exit fullscreen mode

A missing comma can cause a parsing error even when every property name and value is otherwise correct.

4. Invalid JSON Values

JSON supports strings, numbers, booleans, null, objects, and arrays.

It does not support JavaScript-specific values such as undefined, NaN, or function definitions.

Incorrect:

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

Correct:

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

Choose a valid JSON value that matches what your application expects.

A Quick Way to Find the Problem

JavaScript provides JSON.parse() to check whether a string contains valid JSON.

const input = `{
  "name": "Alex",
  "age": 28
}`;

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

If parsing succeeds, the string is syntactically valid JSON. If it fails, JavaScript throws a SyntaxError.

You can also format valid JSON with JSON.stringify():

const formatted = JSON.stringify(
  JSON.parse(input),
  null,
  2
);

console.log(formatted);
Enter fullscreen mode Exit fullscreen mode

The final argument, 2, sets the indentation to two spaces.

One Important Distinction: Syntax vs. Schema

Valid JSON does not automatically mean valid application data.

For example:

{
  "age": "twenty-eight"
}
Enter fullscreen mode Exit fullscreen mode

This is valid JSON. However, an API expecting age to be a number may reject it.

JSON parsing checks syntax. Schema validation checks whether the data follows the structure and types your application requires. They solve different problems.

Final Checklist

Before debugging a JSON parsing issue, check:

  • Are all property names and strings wrapped in double quotes?
  • Are commas placed correctly?
  • Are there any trailing commas?
  • Are all values supported by JSON?
  • Do the braces and brackets match?
  • Does the data also satisfy your application's expected schema?

These simple checks can help narrow down many common JSON errors before you start investigating more complicated problems.


Disclosure: This article was prepared with AI assistance and should be reviewed for technical accuracy by the author before publication.

Top comments (0)