WWeHelpDevs
TutorialJuly 28, 2026·6 min read

How to Format and Validate JSON Like a Pro

Learn how to format, validate, and debug JSON quickly. Covers common JSON errors, best practices for readability, and free tools to speed up your workflow.

JSON is everywhere — REST APIs, config files, databases, logs. Yet most developers waste minutes every week staring at minified JSON blobs trying to spot a missing comma or mismatched bracket. This guide shows you how to format and validate JSON fast.

What is JSON Formatting?

JSON formatting (also called "pretty-printing") adds indentation and line breaks to make compact JSON human-readable. The JSON itself doesn't change — only how it's displayed.

Minified (hard to read):

{"name":"Alice","age":30,"roles":["admin","editor"]}

Formatted (easy to read):

{
  "name": "Alice",
  "age": 30,
  "roles": [
    "admin",
    "editor"
  ]
}

The 5 Most Common JSON Errors

Understanding these errors will save you hours of debugging.

1. Trailing Commas

JSON doesn't allow a comma after the last item in an object or array. This is valid in JavaScript but not in JSON.

// ❌ Invalid
{ "name": "Alice", "age": 30, }

// ✓ Valid
{ "name": "Alice", "age": 30 }

2. Single Quotes Instead of Double Quotes

JSON requires double quotes. Single quotes are not valid.

// ❌ Invalid
{ 'name': 'Alice' }

// ✓ Valid
{ "name": "Alice" }

3. Unquoted Keys

Every key in a JSON object must be a string in double quotes.

// ❌ Invalid
{ name: "Alice" }

// ✓ Valid
{ "name": "Alice" }

4. Comments

JSON has no comment syntax. Adding // or /* */ comments will break parsers.

// ❌ Invalid — JSON has no comments
{
  "port": 3000 // development server port
}

5. Undefined and Functions

JSON only supports strings, numbers, booleans, null, arrays, and objects. No undefined, functions, or Date objects.

// ❌ Invalid
{ "callback": function() {} }

Best Practices for Readable JSON

Use 2-space indentation. It's the most common standard across JavaScript, TypeScript, and Node.js projects. 4 spaces works too, but 2 is the default in JSON.stringify.

Keep nesting shallow. Deeply nested JSON (more than 4 levels) is a design smell. Consider flattening your data model.

Use consistent key naming. Pick camelCase (common in JavaScript) or snake_case (common in Python APIs) and stick to it across your entire API.

Sort keys alphabetically in config files. It makes diffs cleaner and searching easier.

Format JSON in Your Code

In JavaScript and TypeScript:

// Parse and re-format
const formatted = JSON.stringify(JSON.parse(rawJson), null, 2)

// Or format an existing object
const obj = { name: "Alice", age: 30 }
console.log(JSON.stringify(obj, null, 2))

In Python:

import json

raw = '{"name":"Alice","age":30}'
parsed = json.loads(raw)
formatted = json.dumps(parsed, indent=2)
print(formatted)

In the terminal with jq:

cat data.json | jq .
# Or pipe from an API
curl https://api.example.com/users | jq .

Use an Online JSON Formatter

For quick checks during development, use our free JSON Formatter. It validates your JSON in real-time, highlights errors with the exact line and character position, and lets you copy the formatted result instantly — no login required and your data never leaves your browser.

Validating JSON in CI/CD

For production pipelines, add JSON validation to your build process:

# Using jq
cat config.json | jq . > /dev/null && echo "Valid" || echo "Invalid"

# Using Node.js
node -e "JSON.parse(require('fs').readFileSync('config.json', 'utf8'))" && echo "Valid"

# Using Python
python -m json.tool config.json > /dev/null && echo "Valid"

JSON Schema Validation

For more than syntax checking — validating that values match expected types and ranges — use JSON Schema. It lets you define the shape of your JSON and validate against it programmatically.

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "integer", "minimum": 0, "maximum": 150 }
  }
}

Summary

  • Use JSON.stringify(obj, null, 2) in code to format JSON
  • Avoid trailing commas, single quotes, unquoted keys, and comments
  • Use our free JSON Formatter for quick browser-based formatting
  • Add jq to your terminal workflow for API debugging
  • Use JSON Schema for production data validation

The five minutes you spend setting up proper JSON tooling will save you hours of debugging. Good formatting is not cosmetic — it's how you catch bugs before they reach production.

← All articles

Enjoyed this article?

Get new tutorials and guides in your inbox every week. Free, no spam.

Subscribe to WeHelpDevs →