Songtov Utility
Back to Blog

How to Format and Validate JSON: A Complete Guide

Learn everything about JSON formatting and validation — from the basics of JSON syntax to common errors and how to fix them.

CS
Chiho Song

Software Developer · Songtov Utility

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. Originally derived from JavaScript, JSON is now language-independent and supported natively by virtually every modern programming language.

JSON is the backbone of modern APIs, configuration files, and data storage. Whether you are building a REST API, reading a configuration file, or debugging a webhook payload, you will encounter JSON constantly as a developer.

JSON Syntax Basics

JSON is built on two fundamental structures:

  • Objects — an unordered collection of key/value pairs enclosed in curly braces {}. Keys must be strings wrapped in double quotes.
  • Arrays — an ordered list of values enclosed in square brackets [].

Values in JSON can be:

  • A string (e.g., "hello")
  • A number (e.g., 42 or 3.14)
  • A boolean (true or false)
  • null
  • An object or array (nested structures are allowed)

Why Format JSON?

Raw JSON from APIs or logs is often minified — all whitespace stripped to reduce payload size. While compact JSON is efficient for transmission, it is nearly impossible to read at a glance. Formatting (also called "pretty-printing") adds consistent indentation and line breaks so the structure becomes visually clear.

Formatted JSON is invaluable during debugging. When an API returns an unexpected result, the first step is almost always to paste the response into a formatter to understand its structure.

Common Real-World Debugging Scenarios

JSON problems rarely show up as "I am learning JSON." They show up as production bugs, failed integrations, and confusing logs. These are the situations where a formatter becomes useful.

Webhook payload inspection

Payment, messaging, and automation platforms commonly send webhook events as JSON. A single event may contain nested objects for the account, customer, subscription, invoice, metadata, retry count, and event type. When a webhook handler fails, the raw body in the log is often one long line.

Formatting the payload lets you answer practical questions:

  • Is the field missing, or is it nested under another object?
  • Is the value actually null, an empty string, or an empty array?
  • Did the provider send created_at, createdAt, or created?
  • Is the event type the one your handler expects?
  • Is metadata present in test mode but missing in live mode?

This matters because many webhook bugs are mapping bugs. The JSON is valid, but the code reads the wrong path.

API response mismatch

Backend APIs often evolve over time. A frontend may expect user.name, but the backend starts returning user.profile.name. A mobile client may expect an array, but the API returns an object with items and nextCursor. These changes are hard to see in minified output.

Formatted JSON makes shape changes visible. You can compare the response your code expects with the response the server actually returned.

Configuration files and deployment errors

Many tools use JSON or JSON-like configuration: package manifests, compiler settings, build tool configs, service account files, and editor settings. A missing comma or accidental comment can stop a build before application code even runs.

When the error message only says "Unexpected token" or points at a vague line number, validation helps separate syntax problems from application logic problems.

Common JSON Errors and How to Fix Them

JSON has strict syntax rules. Even a single mistake will cause a parser to reject the entire document. Here are the most common errors:

1. Trailing Commas

Unlike JavaScript, JSON does not allow trailing commas after the last item in an object or array. This is valid JavaScript but invalid JSON:

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

The fix is to remove the comma after 30.

2. Single Quotes Instead of Double Quotes

JSON requires double quotes for both keys and string values. Single quotes are not valid:

{ 'name': 'Alice' } // INVALID

3. Unquoted Keys

In JavaScript you can use unquoted object keys, but JSON requires every key to be a double-quoted string:

{ name: "Alice" } // INVALID JSON

4. Comments

JSON does not support comments. If you are editing a config file that uses // comments or /* block comments */, it is likely JSONC (JSON with Comments), not standard JSON.

5. Unescaped Special Characters in Strings

Certain characters inside strings must be escaped with a backslash: " becomes \", newlines become \n, and backslashes become \\.

6. Invisible Characters

Copying JSON from emails, chat tools, PDFs, or rich text editors can introduce invisible characters. Smart quotes, non-breaking spaces, and stray control characters can make a payload fail even when it looks correct on screen.

If a parser rejects a line that appears normal, retype the quotes around the failing key or value, then validate again.

7. JSON vs JavaScript Object Literal

Many examples online show JavaScript objects, not JSON. This is a JavaScript object literal:

{
  name: "Alice",
  active: true,
}

This is valid JSON:

{
  "name": "Alice",
  "active": true
}

The difference matters when pasting examples into API clients, config files, or request bodies.

How to Validate JSON Online

The fastest way to validate JSON is to paste it into an online tool. A good JSON formatter will instantly tell you whether your JSON is valid and, if not, highlight the line with the error. Our free JSON Formatter tool does exactly this — paste your JSON and see a color-coded, indented result or a clear error message in real time.

JSON Formatting in Code

Most programming languages have built-in JSON support. To format JSON in JavaScript, use JSON.stringify with the indent parameter:

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

The third argument (2) specifies the number of spaces to use for indentation. In Python, use json.dumps(data, indent=2).

Debugging Checklist

When a JSON request or response breaks, use this order:

  1. Validate the raw JSON first. If it is not valid JSON, fix syntax before debugging application logic.
  2. Format the payload and inspect the top-level keys.
  3. Check whether the value is missing, null, empty, or in a different nested path.
  4. Confirm naming conventions: snake_case, camelCase, and lowercase keys are not interchangeable.
  5. Compare a working sample against the failing sample.
  6. Check whether your framework already parsed the body, especially in webhook signature verification flows.
  7. Avoid editing production payloads by hand unless you can reproduce the exact request safely.

Conclusion

JSON is simple in theory but easy to get wrong in practice. Understanding its strict syntax rules — double quotes, no trailing commas, no comments — will save you hours of debugging. More importantly, formatting helps you see data shape, naming, nullability, and nesting problems that syntax validation alone cannot explain.

When in doubt, validate first, format second, then debug the application behavior.

Try it yourself

Use our free JSON Formatter

Open JSON Formatter