How to Format and Validate JSON Online — Free & Instant
Working with messy, minified or broken JSON? This guide shows you how to format, beautify and validate JSON instantly — with real examples, common error fixes and best practices.
What Is JSON Formatting?
JSON (JavaScript Object Notation) is the most widely used data format for APIs, configuration files and data exchange. But JSON from APIs often comes back minified — all on one line, impossible to read.
Minified JSON (hard to read):
{"user":{"id":1,"name":"John Doe","email":"[email protected]","roles":["admin","editor"],"active":true}}
Formatted JSON (easy to read):
{
"user": {
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"roles": [
"admin",
"editor"
],
"active": true
}
}
Formatting (also called "pretty printing" or "beautifying") adds indentation and line breaks to make JSON human-readable instantly.
How to Format JSON Online (Easiest Way)
Use DevNova Tools JSON Formatter — it runs entirely in your browser. Your data never leaves your computer.
- Go to devnovatools.com/json/formatter
- Paste your JSON in the input field
- Choose indentation (2 spaces, 4 spaces or tabs)
- See formatted output instantly — copy or download
⚠️ Privacy tip: Never paste sensitive JSON (API keys, tokens, passwords) into tools that send data to a server. DevNova Tools runs 100% client-side — your data stays in your browser.
How to Format JSON in JavaScript
const ugly = '{"name":"John","age":30,"city":"New York"}';
// Parse and format with 2-space indentation
const parsed = JSON.parse(ugly);
const pretty = JSON.stringify(parsed, null, 2);
console.log(pretty);
// Output:
// {
// "name": "John",
// "age": 30,
// "city": "New York"
// }
The third argument to JSON.stringify() controls indentation:
null, 2— 2-space indentation (most common)null, 4— 4-space indentationnull, '\t'— tab indentation
How to Format JSON in Python
import json
ugly = '{"name":"John","age":30,"city":"New York"}'
# Parse and format
parsed = json.loads(ugly)
pretty = json.dumps(parsed, indent=2)
print(pretty)
# Sort keys alphabetically
pretty_sorted = json.dumps(parsed, indent=2, sort_keys=True)
# Format a file
with open('data.json', 'r') as f:
data = json.load(f)
with open('data_formatted.json', 'w') as f:
json.dump(data, f, indent=2)
How to Format JSON in C# / .NET
using System.Text.Json;
string ugly = "{\"name\":\"John\",\"age\":30}";
// Parse and format
var parsed = JsonDocument.Parse(ugly);
var options = new JsonSerializerOptions { WriteIndented = true };
string pretty = JsonSerializer.Serialize(parsed, options);
Console.WriteLine(pretty);
// Using Newtonsoft.Json
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
string formatted = JToken.Parse(ugly)
.ToString(Formatting.Indented);
Console.WriteLine(formatted);
How to Validate JSON
Valid JSON must follow strict rules. Here's what makes JSON invalid:
| Error | Invalid | Valid |
|---|---|---|
| Single quotes | {'name': 'John'} | {"name": "John"} |
| Trailing comma | {"a": 1, "b": 2,} | {"a": 1, "b": 2} |
| Unquoted keys | {name: "John"} | {"name": "John"} |
| Comments | {"a": 1 // comment} | {"a": 1} |
| Undefined value | {"a": undefined} | {"a": null} |
| Missing quotes | {"name": John} | {"name": "John"} |
How to Validate JSON in JavaScript
function isValidJSON(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
// Usage
console.log(isValidJSON('{"name": "John"}')); // true
console.log(isValidJSON('{name: "John"}')); // false
console.log(isValidJSON('{"a": 1,}')); // false — trailing comma
How to Validate JSON in Python
import json
def is_valid_json(text):
try:
json.loads(text)
return True
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
return False
# Usage
print(is_valid_json('{"name": "John"}')) # True
print(is_valid_json('{name: "John"}')) # False
Most Common JSON Errors and How to Fix Them
1. Trailing comma — the most common mistake:
// ❌ Invalid
{
"name": "John",
"age": 30, ← remove this comma
}
// ✅ Valid
{
"name": "John",
"age": 30
}
2. Single quotes instead of double quotes:
// ❌ Invalid
{'name': 'John'}
// ✅ Valid
{"name": "John"}
3. Unquoted property names:
// ❌ Invalid — this is JavaScript object syntax, not JSON
{name: "John"}
// ✅ Valid
{"name": "John"}
4. Comments in JSON:
// ❌ Invalid — JSON does not support comments
{
"name": "John", // user name
"age": 30 /* user age */
}
// ✅ Valid — remove all comments
{
"name": "John",
"age": 30
}
JSON Formatting Best Practices
- ✅ Use 2-space indentation for web/JavaScript projects
- ✅ Use 4-space indentation for Python projects
- ✅ Always use double quotes for strings and keys
- ✅ Sort keys alphabetically for config files — easier to find properties
- ✅ Minify JSON for production API responses — saves bandwidth
- ✅ Validate before deploying — invalid JSON breaks everything silently
- ❌ Never put comments in JSON — use YAML or JSONC if you need comments
- ❌ Never use trailing commas — not valid in standard JSON
JSON vs JSONC vs JSON5
| Format | Comments | Trailing commas | Use case |
|---|---|---|---|
| JSON | ❌ | ❌ | APIs, data exchange |
| JSONC | ✅ | ✅ | VS Code config files |
| JSON5 | ✅ | ✅ | Config files, human-written JSON |
Frequently Asked Questions
What is the difference between JSON formatting and JSON validation?
Formatting adds indentation and line breaks for readability. Validation checks if the JSON is syntactically correct. Our JSON Formatter does both simultaneously.
Why is my JSON invalid?
The most common reasons are: trailing commas, single quotes instead of double quotes, unquoted keys, or JavaScript-style comments. Paste your JSON into our JSON Validator to see the exact error and line number.
Is it safe to format JSON online?
Only if the tool runs client-side. DevNova Tools JSON Formatter runs 100% in your browser — your JSON never leaves your computer. Never paste API keys or sensitive data into tools that send data to a server.
How do I format a large JSON file?
For files under 5MB, paste directly into our formatter. For larger files, use Python's json.tool module from the command line:
python -m json.tool large-file.json > formatted.json
What is JSON minification?
Minification removes all whitespace to make JSON as small as possible — the opposite of formatting. Use our JSON Minifier for production API responses to reduce bandwidth.
Can I format JSON from the command line?
Yes — several ways:
# Python
echo '{"name":"John"}' | python -m json.tool
# Node.js
node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')),null,2))"
# jq (install separately)
echo '{"name":"John"}' | jq .
Paste any JSON and get it formatted instantly. Runs in your browser — your data never leaves your computer.
JSON Formatter →