JSON Formatter and Validator — Pretty Print, Minify, and Fix JSON Online
JSON is the universal data format of the modern web. Every API response, configuration file, and data exchange between services uses it. But raw JSON from APIs is almost always minified — a single unreadable line of text that is impossible to debug without formatting. A JSON formatter turns that wall of text into properly indented, syntax-highlighted, navigable structure in seconds.
This guide covers what JSON formatting and validation do, how to use them effectively, how to work with JSON programmatically in multiple languages, and the most common JSON errors developers encounter.
What Is JSON Formatting?
JSON formatting (also called pretty-printing or beautifying) takes compact, minified JSON and adds whitespace, indentation, and line breaks to make it human-readable. Validation checks whether a JSON string conforms to the JSON specification (ECMA-404) — that all keys are double-quoted, all values are valid types, no trailing commas exist, and the structure is properly nested.
You would use JSON formatting when debugging API responses, inspecting configuration files like package.json or tsconfig.json, reviewing MongoDB documents, comparing data structures, or documenting API schemas. Minification is the reverse — removing all whitespace to produce the smallest possible payload for transmission or storage.
How to Format JSON with FlipMyCase
- Open the FlipMyCase JSON Formatter.
- Paste your JSON — the tool instantly formats, validates, and syntax-highlights it.
- Use the tree view to navigate nested objects and arrays. Click any value to copy its JSON path.
- Toggle between 2-space and 4-space indentation.
- Use the Minify button to compress JSON for API payloads.
If your JSON has errors, the validator pinpoints the exact line and character position with a description of the problem.
Code Examples for JSON Handling
JavaScript
// Pretty-print JSON
const data = {"users":[{"id":1,"name":"Alice","email":"alice@example.com"},{"id":2,"name":"Bob","email":"bob@example.com"}]};
console.log(JSON.stringify(data, null, 2));
// {
// "users": [
// {
// "id": 1,
// "name": "Alice",
// "email": "alice@example.com"
// },
// ...
// Minify JSON
const pretty = `{
"name": "Alice",
"age": 30
}`;
console.log(JSON.stringify(JSON.parse(pretty)));
// {"name":"Alice","age":30}
// Validate JSON safely
function isValidJSON(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
console.log(isValidJSON('{"name": "Alice"}')); // true
console.log(isValidJSON('{name: "Alice"}')); // false (unquoted key)
Python
import json
# Pretty-print JSON
data = {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}
formatted = json.dumps(data, indent=2, sort_keys=True)
print(formatted)
# {
# "users": [
# {
# "id": 1,
# "name": "Alice"
# },
# ...
# Read and format a JSON file
with open('config.json', 'r') as f:
data = json.load(f)
with open('config_formatted.json', 'w') as f:
json.dump(data, f, indent=2)
# Validate JSON
def is_valid_json(text):
try:
json.loads(text)
return True
except json.JSONDecodeError as e:
print(f'Invalid JSON at line {e.lineno}, column {e.colno}: {e.msg}')
return False
is_valid_json('{"name": "Alice",}')
# Invalid JSON at line 1, column 18: Expecting property name enclosed in double quotes
Go
package main
import (
"bytes"
"encoding/json"
"fmt"
)
func main() {
// Pretty-print JSON
compact := []byte(`{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}`)
var prettyJSON bytes.Buffer
json.Indent(&prettyJSON, compact, "", " ")
fmt.Println(prettyJSON.String())
// Minify JSON
pretty := []byte("{\n \"name\": \"Alice\",\n \"age\": 30\n}")
var minified bytes.Buffer
json.Compact(&minified, pretty)
fmt.Println(minified.String())
// {"name":"Alice","age":30}
// Validate JSON
var js json.RawMessage
if err := json.Unmarshal(compact, &js); err != nil {
fmt.Println("Invalid JSON:", err)
} else {
fmt.Println("Valid JSON")
}
}
Real-World Use Cases
API debugging. You make an API call and get back a 2,000-character single-line response. Without formatting, finding a specific field is like searching for a needle in a haystack. Paste the response into the JSON Formatter, use the tree view to navigate the structure, and click the field you need to copy its JSON path. Then use that path in your code.
Configuration file management. Files like package.json, tsconfig.json, .eslintrc.json, and Kubernetes manifests need to be both machine-parseable and human-readable. A formatter ensures consistent indentation after manual edits, and validation catches syntax errors before they cause deployment failures.
Data comparison. You need to compare the JSON response from staging and production environments. Format both responses with consistent indentation, then use the Text Diff tool to see exactly what differs. This workflow catches missing fields, changed values, and structural differences instantly.
Documentation. API documentation needs clean, readable JSON examples. Format your sample responses with 2-space indentation, validate them to ensure they are actually parseable, and include them in your docs. Invalid JSON examples in documentation are embarrassingly common.
Common Mistakes and Gotchas
Trailing commas are the number one JSON error. JavaScript objects allow trailing commas, but JSON does not. {"name": "Alice", "age": 30,} is invalid JSON. Remove the comma after the last property in every object and array.
Single quotes are another common mistake. JSON requires double quotes for both keys and string values. {'name': 'Alice'} is valid JavaScript but invalid JSON. Always use double quotes.
Unquoted keys work in JavaScript but fail in JSON. {name: "Alice"} must be {"name": "Alice"}. Every key must be a double-quoted string.
Comments are not allowed in JSON. {"name": "Alice" /* user name */} is invalid. If you need comments in configuration files, use JSONC (JSON with Comments) or JSON5, but be aware that standard JSON parsers will reject them.
Number formatting catches some people. Leading zeros (01), trailing decimal points (1.), and hex literals (0xFF) are all invalid in JSON. Use standard decimal notation: 1, 1.0, 255.
Frequently Asked Questions
What is the difference between JSON and a JavaScript object?
JSON is a strict subset of JavaScript object syntax. JSON requires double-quoted keys, does not allow trailing commas, does not support comments, does not allow undefined or functions as values, and only supports double-quoted strings. A JavaScript object is more flexible but cannot be directly transmitted as JSON without serialization via JSON.stringify().
Should I use 2-space or 4-space indentation? Two-space indentation is the most common convention for JSON in the JavaScript ecosystem (npm, ESLint, and Prettier all default to it). Four-space indentation is more common in Python-ecosystem tools. Pick one and be consistent across your project. The JSON Formatter supports both.
How do I convert between JSON and other formats? For JSON to YAML conversion, use a dedicated converter. For JSON to CSV, you need to flatten the nested structure first. The CSV to JSON tool handles the reverse direction. For encoding JSON strings for URLs or HTML, use the String Encoder.
Is it safe to paste sensitive data into the formatter? Yes, when using the FlipMyCase JSON Formatter. All processing happens in your browser — no data is transmitted to any server. The tool works offline as a PWA, so you can verify this by disconnecting from the internet and confirming it still works.
Conclusion
JSON formatting and validation are essential developer tools that save hours of debugging time. Whether you are inspecting API responses, cleaning up configuration files, or preparing documentation examples, a good formatter turns unreadable JSON into navigable, validated structure.
For everyday use, the FlipMyCase JSON Formatter provides instant formatting, validation, tree view, path copying, and minification in your browser. For programmatic use, the JavaScript, Python, and Go examples above handle formatting, minification, and validation in your codebase. Combine with the Text Diff tool for comparing JSON structures, or the String Encoder for encoding JSON payloads.