JSON Formatter
Validation
—
Understanding JSON: Syntax, Data Types, Validation, and Best Practices
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that has become the universal language of web APIs, configuration files, and data storage. Created by Douglas Crockford in the early 2000s and standardized as ECMA-404 and RFC 8259, JSON's success stems from its simplicity: it uses a small set of syntax rules that map directly to data structures found in virtually every programming language. Over 90% of public web APIs use JSON as their primary response format, and it has largely replaced XML for most modern data interchange tasks.
This free JSON formatter and validator lets you paste raw JSON, instantly check whether it is valid, and see it pretty-printed with proper indentation. All processing happens locally in your browser — your data is never sent to any server, making this tool safe for formatting sensitive API responses, configuration files, or database exports.
JSON Syntax Rules: The Complete Reference
JSON syntax is a strict subset of JavaScript object literal syntax, with several important restrictions that trip up even experienced developers. Understanding these rules will help you avoid the most common validation errors.
Objects are enclosed in curly braces {} and contain zero or more key-value pairs separated by commas. Every key must be a double-quoted string — no single quotes, no unquoted identifiers. Values are separated from keys by a colon. Arrays are enclosed in square brackets [] and contain zero or more values separated by commas. Arrays can hold values of any type, including mixed types within the same array.
JSON Data Types
| Type | Example | Rules |
|---|---|---|
| String | "hello world" | Must use double quotes. Supports \n, \t, \", \\, \uXXXX escapes. |
| Number | 42, 3.14, -1e10 | No leading zeros, no hex, no NaN, no Infinity. Supports integers, decimals, exponents. |
| Boolean | true, false | Lowercase only. Not "true" or TRUE. |
| Null | null | Lowercase only. Represents absent value. |
| Object | {"key": "value"} | Keys must be double-quoted strings. No trailing comma. |
| Array | [1, "two", null] | Ordered list. Mixed types allowed. No trailing comma. |
Notably absent from JSON's type system: Date — dates are represented as ISO 8601 strings like "2026-03-26T12:00:00Z". undefined — does not exist in JSON; use null instead. Comments — JSON provides no comment syntax. If you need comments, consider JSON5 or JSONC (JSON with Comments), used by VS Code configuration files. Functions — not a valid JSON value.
The Most Common JSON Errors (and How to Fix Them)
Trailing commas: JavaScript allows {"a": 1, "b": 2,} but JSON does not. Remove the comma after the last element. This is the single most common JSON validation error.
Single quotes: {'name': 'John'} is valid JavaScript but invalid JSON. All strings and keys must use double quotes: {"name": "John"}.
Unquoted keys: {name: "John"} is valid JavaScript but invalid JSON. Keys must always be double-quoted strings.
Comments: // this is not allowed and /* neither is this */ will cause parsing failures. JSON has no comment syntax by design.
Special number values: NaN, Infinity, and -Infinity are not valid JSON numbers. Use null or a string representation instead.
Unescaped control characters: Literal tabs, newlines, and other control characters inside strings must be escaped as \t, \n, etc.
JSON vs XML: A Detailed Comparison
| Feature | JSON | XML |
|---|---|---|
| Verbosity | Compact (30-50% smaller) | Verbose (opening + closing tags) |
| Data types | String, Number, Boolean, Null, Object, Array | Everything is text (schemas add types) |
| Arrays | Native support with [] | Repeated elements (no native array) |
| Comments | Not supported | Supported with <!-- --> |
| Attributes | No (use nested objects) | Yes |
| Namespaces | No | Yes |
| Schema validation | JSON Schema (separate spec) | XSD, DTD, RelaxNG (mature) |
| Parsing | JSON.parse() — fast, simple | DOM/SAX parsers — more complex |
| Primary use | Web APIs, config files, NoSQL | Enterprise, SOAP, document formats |
JSON in the Wild: Where You Encounter JSON Every Day
REST APIs: When your browser or mobile app makes requests to services like Twitter, Stripe, or Google Maps, the response is almost always JSON. API responses contain nested objects, arrays of records, and metadata — all structured in JSON format.
Configuration files: package.json (npm/Node.js), tsconfig.json (TypeScript), .eslintrc.json (ESLint), appsettings.json (.NET), and composer.json (PHP) all use JSON to store project configuration. VS Code settings use JSONC, which extends JSON with comments.
NoSQL databases: MongoDB stores documents as BSON (Binary JSON), and CouchDB uses pure JSON for both storage and queries. Firebase Realtime Database stores its entire data tree as JSON. DynamoDB represents items as JSON-like attribute maps.
JSON-LD and Schema.org: Structured data for search engines (the Schema.org markup that powers Google rich results) is typically embedded in web pages as JSON-LD script tags — in fact, every page on WorldlyCalc uses JSON-LD for its FAQ and breadcrumb schema markup.
Logging and analytics: Modern logging systems like ELK Stack (Elasticsearch, Logstash, Kibana), Datadog, and Splunk prefer structured JSON logs over plain text because JSON makes fields queryable, filterable, and aggregatable.
Tips for Working with JSON Effectively
Always validate before parsing: Wrap JSON.parse() in a try-catch block. Invalid JSON will throw a SyntaxError with a helpful message indicating the position of the error — this is exactly what this formatter displays.
Use JSON.stringify() for pretty-printing: JSON.stringify(obj, null, 2) formats JSON with 2-space indentation. The second parameter (replacer) can filter which keys to include; the third parameter controls indentation.
Watch for large numbers: JSON numbers are parsed as JavaScript floating-point numbers, which lose precision for integers larger than 2^53 - 1 (9,007,199,254,740,991). APIs returning large IDs (like Twitter's snowflake IDs) should send them as strings to avoid precision loss.
Handle dates carefully: JSON has no date type. The convention is to use ISO 8601 strings ("2026-03-26T12:00:00Z") or Unix timestamps (integers). Always document which format your API uses.
Frequently Asked Questions
What is JSON and why is it the standard data format for web APIs?
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for both humans and machines to read and write. It became the dominant API format because it maps directly to data structures in most programming languages (objects/dictionaries, arrays/lists, strings, numbers, booleans, null), is less verbose than XML, and is natively parseable in JavaScript with a single function call. Over 90% of public web APIs use JSON as their primary response format, and it is supported by every modern programming language.
What are the most common JSON syntax errors?
The most frequent JSON errors are: trailing commas after the last item in an array or object (not allowed in JSON), using single quotes instead of double quotes for strings and keys, including comments (JSON does not support comments), unquoted property names, using undefined or NaN values (not valid JSON), and unescaped control characters inside strings. This formatter catches all of these errors and displays the exact error message from the parser.
What is the difference between JSON and JavaScript objects?
While JSON syntax is derived from JavaScript object literals, they are not identical. JSON requires all keys to be double-quoted strings, does not support trailing commas, does not allow comments, forbids undefined and function values, and requires string values to use double quotes only. A JavaScript object literal is more permissive — keys can be unquoted identifiers, values can include functions and undefined, and trailing commas are allowed. JSON.parse() enforces strict JSON rules, while eval() would accept JavaScript objects (but should never be used for security reasons).
How does JSON compare to XML for data interchange?
JSON is more compact (typically 30-50% smaller), faster to parse, and maps more naturally to programming language data structures. XML supports attributes, mixed content (text interleaved with elements), namespaces for avoiding naming conflicts, and mature schema languages (XSD, DTD, RelaxNG). JSON dominates web APIs and modern applications, while XML remains common in legacy enterprise systems, SOAP web services, and document-oriented formats like SVG, XHTML, and RSS/Atom feeds.
Is my data safe when I paste it into this JSON formatter?
Yes, all processing happens entirely in your browser using JavaScript's built-in JSON.parse() and JSON.stringify() functions. Your data is never sent to any server, stored in any database, or transmitted over the network. The formatting and validation run locally on your device, making this tool safe for formatting sensitive API responses, configuration files containing credentials (though you should avoid pasting actual secrets), database exports, and any other private JSON data. You can verify this by using your browser's developer tools network tab while formatting.
What is JSON Schema and how is it used for validation?
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents beyond basic syntax checking. While this formatter checks that your JSON is syntactically valid (proper quotes, brackets, and data types), JSON Schema lets you define structural rules: required fields, data types for specific properties, minimum and maximum values, string patterns (via regex), array length constraints, and more. JSON Schema is widely used in API documentation (OpenAPI/Swagger), form validation, and configuration file validation. Major libraries like Ajv (JavaScript) and jsonschema (Python) implement JSON Schema validation.