How to Format and Validate JSON Online

How to quickly format, pretty-print, and validate JSON — in the browser, terminal, and your editor. Plus the most common JSON syntax errors and how to fix them.

Try it yourself

Use our free JSON Formatter — no sign-up, runs in your browser.

Open tool →

JSON is everywhere — API responses, config files, logs, databases. And broken JSON is one of the most common things developers deal with. This guide covers how to format and validate JSON quickly, the errors you’ll hit most often, and how to fix them.

What does “formatting” JSON mean?

Raw JSON from an API or log file often looks like this:

{"user":{"id":1,"name":"Jane Doe","email":"jane@example.com","roles":["admin","editor"],"active":true}}

Formatted (pretty-printed) JSON adds whitespace and indentation to make it readable:

{
  "user": {
    "id": 1,
    "name": "Jane Doe",
    "email": "jane@example.com",
    "roles": [
      "admin",
      "editor"
    ],
    "active": true
  }
}

Both are semantically identical. Formatting is purely for human readability.

How to format JSON online

The fastest way: paste into our JSON Formatter and click Format JSON. It formats the JSON, validates it, and shows you exactly where the error is if something’s wrong — with line and column numbers.

How to format JSON in the terminal

Using jq (the most powerful JSON tool for the CLI):

# Pretty-print a JSON file
jq . file.json

# Pretty-print from an API response
curl -s https://api.example.com/users | jq .

# Minify
jq -c . file.json

Using Python (available everywhere, no install needed):

# Pretty-print
cat file.json | python3 -m json.tool

# Or from a string
echo '{"a":1,"b":2}' | python3 -m json.tool

Using Node.js:

node -e "const fs=require('fs'); console.log(JSON.stringify(JSON.parse(fs.readFileSync('/dev/stdin','utf8')),null,2))"

How to format JSON in your editor

VS Code: Open a .json file, press Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac).

JetBrains IDEs: Ctrl+Alt+L / Cmd+Option+L.

Vim: :%!python3 -m json.tool

Minifying JSON

Minified JSON strips all whitespace to reduce file size — useful before sending data over a network or storing in a database field.

// JavaScript
JSON.stringify(JSON.parse(jsonString))  // removes all whitespace

// With jq
jq -c . file.json

// With Python
python3 -c "import json,sys; print(json.dumps(json.load(sys.stdin)))" < file.json

Our formatter’s Minify JSON button does this in one click.

The most common JSON syntax errors

Missing or extra comma

// ❌ Trailing comma — not valid in JSON (unlike JavaScript)
{
  "name": "Jane",
  "age": 30,
}

// ✅ Correct
{
  "name": "Jane",
  "age": 30
}
// ❌ Missing comma between items
{
  "name": "Jane"
  "age": 30
}

Unquoted keys

JSON requires double quotes around all keys. Unquoted keys are JavaScript object syntax, not JSON.

// ❌ JavaScript object literal — not JSON
{ name: "Jane", age: 30 }

// ✅ Valid JSON
{ "name": "Jane", "age": 30 }

Single quotes

JSON only allows double quotes. Single quotes are a common mistake when copying from JavaScript code.

// ❌ Single quotes
{ 'name': 'Jane' }

// ✅ Double quotes
{ "name": "Jane" }

Unclosed braces or brackets

// ❌ Missing closing brace
{
  "settings": {
    "theme": "dark"
}

Comments

JSON does not support comments. Trying to add them is a syntax error.

// ❌ Comments are not valid JSON
{
  // User settings
  "theme": "dark"
}

If you need comments in config files, consider JSON5 or JSONC (used in VS Code settings), but these are not standard JSON and won’t parse with JSON.parse.

Invalid values

JSON has a strict set of value types: strings, numbers, booleans (true/false), null, objects, and arrays. undefined, functions, and NaN are not valid JSON values.

// ❌ undefined and NaN are not JSON values
{ "value": undefined }
{ "result": NaN }

// ✅ Use null instead of undefined
{ "value": null }

Reading JSON error messages

Modern browsers give helpful error messages. For example:

Expected ',' or '}' after property value in JSON at position 45 (line 4, column 3)

This tells you exactly what the parser expected and where it got confused. Jump to that line in your editor and look for a missing comma or bracket.

Our JSON Formatter surfaces these browser errors with line and column numbers directly in the UI.

Validating JSON against a schema

If you need to validate that JSON conforms to a specific structure (right fields, right types), you need JSON Schema — a standard for describing the shape of JSON data.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string" },
    "age":  { "type": "integer", "minimum": 0 }
  }
}

Tools like AJV (JavaScript) and jsonschema (Python) validate JSON against schemas programmatically.

JSON in JavaScript

// Parse a JSON string into a JavaScript object
const obj = JSON.parse('{"name":"Jane","age":30}');

// Convert a JavaScript object to a JSON string
const json = JSON.stringify(obj);

// Pretty-print with 2-space indent
const pretty = JSON.stringify(obj, null, 2);

// Handle parse errors gracefully
try {
  const data = JSON.parse(userInput);
} catch (e) {
  console.error('Invalid JSON:', e.message);
}

JSON in Python

import json

# Parse JSON string
obj = json.loads('{"name": "Jane", "age": 30}')

# Read from file
with open('data.json') as f:
    obj = json.load(f)

# Serialize to string
json_str = json.dumps(obj, indent=2)

# Write to file
with open('output.json', 'w') as f:
    json.dump(obj, f, indent=2)

Ready to try it?

Free, client-side JSON Formatter — nothing sent to a server.

Open JSON Formatter →