DEV Community

Cover image for # JSON Validation: Common Errors and How to Find Them
Chhatrapal Singh
Chhatrapal Singh

Posted on

# JSON Validation: Common Errors and How to Find Them

A JSON response can look almost correct while still being invalid.

One small syntax mistake can prevent an API response, configuration file, or application from parsing correctly.

Let's look at some of the most common problems.

1. Missing Commas

This is invalid:

{
  "name": "John"
  "age": 30
}
Enter fullscreen mode Exit fullscreen mode

There needs to be a comma between the properties:

{
  "name": "John",
  "age": 30
}
Enter fullscreen mode Exit fullscreen mode

2. Using Single Quotes

JSON requires double quotes for strings.

Incorrect:

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

Correct:

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

3. Trailing Commas

This is another common mistake:

{
  "name": "John",
  "age": 30,
}
Enter fullscreen mode Exit fullscreen mode

Standard JSON does not allow the trailing comma.

4. Unclosed Brackets

Nested JSON makes this particularly easy to miss:

{
  "user": {
    "name": "John"
}
Enter fullscreen mode Exit fullscreen mode

The nested object needs to be closed properly.

Why Validation Matters

When you're working with a large JSON document, manually finding these errors can be frustrating.

A JSON validator can quickly determine whether your document follows JSON syntax rules.

For quick validation, you can use:

https://jsonformatterkit.com/

JSON vs JavaScript Objects

It's also important to remember that JSON and JavaScript object syntax aren't exactly the same.

JavaScript may allow syntax that standard JSON does not.

For example:

const user = {
  name: "John"
};
Enter fullscreen mode Exit fullscreen mode

JSON requires:

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

Conclusion

When JSON isn't parsing correctly, check:

  • Quotes
  • Commas
  • Brackets
  • Braces
  • Data types
  • Nested structures

A validator can save time when working with large or complicated JSON documents.

Top comments (0)