Developer Tools

Mastering JSON Formatting and Validation

Why perfectly formatted JSON matters down to the last comma, and how to spot common structural errors.

5 min read

Code editor showing JSON

Javascript Object Notation (JSON) is the undeniable king of modern APIs and data transfer. However, because it's so strict, a single misplaced comma can crash an entire production endpoint.

In this guide, we'll cover the golden rules of JSON formatting.

The Syntax Rules of JSON

Unlike normal Javascript objects, JSON is notoriously unforgiving. If you break these rules, the parser throws a SyntaxError.

  1. Keys MUST be wrapped in double quotes: 'name': 'John' is invalid. It must be "name": "John".
  2. No trailing commas: You cannot leave a comma after the final item in an array or object.
  3. No functions or undefined: JSON can only store strings, numbers, booleans, arrays, objects, and null.

Valid vs. Invalid JSON

Let's look at what the strict JSON parser requires from us.

{
  "user": {
    "id": 1042,
    "isActive": true,
    "roles": ["admin", "editor"],
    "preferences": null
  }
}

If we tried to write that same object loosely like we do in JS scripts:

// This is INVALID JSON (but valid Javascript)
{
  user: {
    id: 1042,
    isActive: true, // trailing comma
  }
}

Why Validation Matters

When connecting microservices, passing invalid JSON means the receiving server will throw a massive 500 error because JSON.parse() fails catastrophically. Always lint and format your JSON before adding it to an application.

Use an automated formatting tool to validate that your API payloads are secure and structurally sound!

Ready to test your data? Jump over to our JSON Formatter & Validator to instantly prettify your raw strings!