What Is JSON? Formatting and Validation Guide
What is JSON?
JSON — short for JavaScript Object Notation — is a lightweight data interchange format that has become the backbone of modern web development. Despite its name, JSON is language-independent and used across virtually every programming language, from Python and Go to Rust and C#.
If you've ever made an API call, stored configuration data, or worked with a NoSQL database, you've almost certainly worked with JSON. It was originally derived from JavaScript object syntax by Douglas Crockford in the early 2000s and quickly became the preferred alternative to XML for data exchange.
JSON Syntax: The Basics
A JSON document is built from two structures:
- Objects — unordered collections of key-value pairs, wrapped in curly braces
{} - Arrays — ordered lists of values, wrapped in square brackets
[]
Here's a simple example:
{
"name": "Alice",
"age": 30,
"isActive": true,
"skills": ["JavaScript", "Python", "SQL"],
"address": {
"city": "Austin",
"state": "TX"
}
}
JSON supports six data types: strings, numbers, booleans (true/false), null, objects, and arrays. That's it — no dates, no functions, no comments (a common source of frustration).
Why JSON Formatting Matters
Raw JSON from APIs or logs is often minified — compressed into a single line with no whitespace. This is great for network efficiency but terrible for readability:
{"name":"Alice","age":30,"isActive":true,"skills":["JavaScript","Python","SQL"],"address":{"city":"Austin","state":"TX"}}
Try debugging a 500-line JSON response that looks like that. Proper formatting (also called "pretty-printing") adds indentation and line breaks so you can actually read the structure.
Common formatting issues developers face
- Trailing commas — JSON does not allow trailing commas after the last item in an object or array. This is probably the most common syntax error.
- Single quotes — JSON requires double quotes for strings.
'name'is invalid;"name"is correct. - Unquoted keys — Unlike JavaScript objects, JSON keys must always be quoted.
- Comments — JSON does not support comments. If you need comments, consider JSONC or JSON5.
How to Validate JSON
Validation ensures your JSON is syntactically correct before you try to parse it. Invalid JSON will cause JSON.parse() to throw an error in JavaScript, and similar failures in other languages.
Here's how you can validate JSON programmatically:
JavaScript:
try {
const data = JSON.parse(jsonString);
console.log("Valid JSON");
} catch (e) {
console.error("Invalid JSON:", e.message);
}
Python:
import json
try:
data = json.loads(json_string)
print("Valid JSON")
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
Command line (with jq):
echo '{"name": "Alice"}' | jq .
JSON vs Other Formats
| Format | Best For | Drawback |
|---|---|---|
| JSON | APIs, config, data exchange | No comments, no dates |
| YAML | Config files, Kubernetes | Whitespace-sensitive, complex |
| XML | Legacy systems, SOAP | Verbose, harder to parse |
| TOML | Config files | Less common, limited nesting |
JSON wins for API communication because it's simple, universally supported, and maps directly to native data structures in most languages.
Practical Tips for Working with JSON
Use a formatter. Whether you're inspecting API responses, debugging webhook payloads, or cleaning up config files, a JSON formatter saves time and catches errors instantly. You can format and validate JSON instantly with our free tool — just paste your JSON and get clean, indented output with syntax highlighting.
Validate before sending. If you're building an API, validate outgoing JSON to avoid sending malformed responses. Most frameworks handle this automatically, but custom serialization can introduce bugs.
Watch for encoding. JSON must be UTF-8 (or UTF-16/32, though UTF-8 is standard). Special characters in strings need to be escaped: \", \\, \n, \t, etc.
Use schema validation for complex data. JSON Schema lets you define the expected structure of your JSON documents — required fields, data types, value ranges, and patterns. Tools like Ajv (JavaScript) or jsonschema (Python) can validate data against a schema at runtime.
When JSON Isn't the Right Choice
JSON is excellent for structured data exchange, but it's not always the best fit:
- Binary data — JSON can't efficiently represent binary. Use Base64 encoding within JSON or choose a binary format like Protocol Buffers or MessagePack.
- Streaming data — Standard JSON requires the entire document to be parsed. For streaming, consider NDJSON (newline-delimited JSON) where each line is a separate JSON object.
- Highly relational data — If your data has complex relationships, a relational database with SQL might be more appropriate than nested JSON documents.
Wrapping Up
JSON is deceptively simple — two structures, six types, and a handful of syntax rules. But that simplicity is exactly what makes it powerful. Understanding how to properly format, validate, and work with JSON is a fundamental skill that pays off every day as a developer.
Next time you're staring at a wall of minified JSON, don't squint — use a proper formatting tool to make it readable in one click.
Try the tool mentioned in this article:
Open Tool →