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.

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 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 \\.

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).

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. When in doubt, run your JSON through a formatter and validator to catch errors instantly.

Try it yourself

Use our free JSON Formatter

Open JSON Formatter