DevToolsHub
← Back to Home

JSON Formatting 101: How to Debug JSON Data Like a Pro

June 19, 20266 min readTutorial

JSON (JavaScript Object Notation) is the lingua franca of modern web APIs. Whether you are debugging a REST endpoint, configuring a cloud service, or building a frontend app, you encounter JSON every day. Yet reading raw, minified JSON is a painful experience — one missing comma can break an entire payload. Here is everything you need to know about JSON formatting, validation, and debugging.

What Is JSON Formatting?

JSON formatting (also called "pretty-printing") transforms compressed, hard-to-read JSON into an indented, human-readable structure. A JSON formatter takes something like this:

{"name":"DevToolsHub","tools":[{"name":"JSON Formatter","url":"/json-formatter"},{"name":"Base64","url":"/base64"}],"active":true}

And turns it into this:

{
  "name": "DevToolsHub",
  "tools": [
    {
      "name": "JSON Formatter",
      "url": "/json-formatter"
    },
    {
      "name": "Base64",
      "url": "/base64"
    }
  ],
  "active": true
}

Why Formatting Matters

  • Find errors faster — malformed JSON is immediately obvious when you can see the structure
  • Compare responses — formatted JSON makes side-by-side comparison of API outputs easy
  • Share readable snippets — formatted JSON is easier to paste into documentation, issues, or PRs
  • Debug configurations — many cloud and DevOps tools (Terraform, Kubernetes, AWS) output JSON

Common JSON Mistakes to Watch For

1. Trailing Commas

JavaScript allows trailing commas in objects and arrays. JSON does not. This is one of the most common sources of parse errors:

// Invalid JSON (trailing comma)
{
  "name": "DevToolsHub",  ← remove this comma
}

2. Unquoted Keys

In JavaScript, object keys can be unquoted identifiers. JSON requires all keys to be wrapped in double quotes:

// Invalid JSON (unquoted key)
{ name: "DevToolsHub" }

// Valid JSON
{ "name": "DevToolsHub" }

3. Single Quotes Instead of Double Quotes

JSON only allows double quotes ("). Single quotes (') are not valid, even though many programming languages accept them for string literals.

4. Undefined or NaN Values

JSON supports null, true, and false, but not undefined or NaN. These values cause JSON.stringify() to silently drop keys or convert them to null.

JSON Formatting Best Practices

Use 2-Space Indentation

The standard for JSON formatting is 2-space indentation. It provides enough visual structure without wasting horizontal space. Some tools default to 4 spaces — configure them to use 2 for consistency with most API documentation.

Validate Before You Use

Always validate JSON before feeding it to your application. A good JSON formatter with validation catches errors immediately and shows you exactly where the problem is. This saves hours of debugging.

Compress for Production

When sending JSON over the wire, use the compression mode to strip all whitespace. JSON.stringify(value) (without spacing arguments) produces compressed output. A typical API response shrinks by 30-50% when compressed — saving bandwidth and improving load times.

Putting It All Together

Whether you are a seasoned backend engineer or a frontend developer learning the ropes, mastering JSON formatting is a foundational skill. The next time you copy a curl response, paste it into a JSON formatter before trying to read it. Your eyes — and your debugging efficiency — will thank you.

Practical Tips for Faster JSON Debugging

  • Pretty-print from the consoleJSON.stringify(data, null, 2) instantly formats any object in DevTools
  • Pipe API output through jqcurl ... | jq . validates and formats in one step
  • Validate before you trust — a formatter that flags errors with line numbers saves more time than any other feature
  • Compress for the wire, format for the eyes — strip whitespace for API responses, pretty-print for logs and docs
// Format any object in the browser console
console.log(JSON.stringify(data, null, 2));

// Validate and pretty-print from the command line
curl https://api.example.com/data | jq .

Frequently Asked Questions

What is the difference between JSON and a JavaScript object?
JSON is a strict text format: keys must be double-quoted, strings use double quotes, and trailing commas are forbidden. A JavaScript object literal is code, not text — it allows unquoted keys, single quotes, functions, and trailing commas. JSON.stringify() and JSON.parse() bridge the two.
How do I check if a JSON string is valid?
Wrap it in JSON.parse() inside a try/catch block, or paste it into a JSON formatter with validation. A good validator reports the exact line and character where parsing fails, which makes the fix obvious.
Does JSON support comments?
No. The JSON specification does not allow comments of any kind. If you need comments in configuration, consider a superset like JSON5, or a format designed for it like YAML or TOML.
Is formatting large JSON files slow?
Formatting is a linear pass, so even multi-megabyte files usually process in milliseconds. The bottleneck is usually the browser tab or editor, not the formatter. For huge files, prefer streaming tools or jq over pasting into a web page.
Why does JSON.stringify() drop some of my object keys?
JSON.stringify() silently omits keys whose values are undefined, functions, or symbols, and converts NaN and Infinity to null. If keys disappear, check for those value types or provide a replacer function to control serialization.