Introducing JSON
JSON (JavaScript Object Notation) is a lightweight, text-based format for exchanging data. It’s easy for people to read and write, and easy for programs to parse and generate. Although its syntax comes from JavaScript, JSON is language-independent and used broadly across the C-family of languages and beyond. These qualities make JSON a natural choice for APIs, configuration, logging, and storage.
Two core structures
- Object: An unordered set of name–value pairs, written with
{and}. Names are strings followed by a colon, pairs separated by commas. - Array: An ordered list of values, written with
[and], items separated by commas. - Values: string (double-quoted), number,
true,false,null, object, or array. These can be nested. - Strings: sequences of Unicode characters in double quotes; backslash escapes are allowed.
- Whitespace: can appear between tokens.
![Diagram of JSON building blocks: objects { }, arrays [ ], and allowed value types](/images/compressed_jsonBuildingBlocks.webp)
What is JSON Formatting and Validation?
JSON (JavaScript Object Notation) is a lightweight data-interchange format used extensively in APIs, configurations, and data storage. Formatting makes JSON human-readable with indentation and line breaks, while validation ensures it adheres to syntax rules, preventing errors in applications.
Proper formatting aids debugging and collaboration, and validation catches issues like missing quotes or trailing commas early. Tools like JSON.parse() in JavaScript or libraries like ajv provide these capabilities.
- Improves readability for developers and teams.
- Detects syntax errors instantly.
- Minification reduces file size for production.
- Schema validation ensures data structure compliance.
Best Practices and Common Pitfalls
Always validate JSON before use in production to avoid runtime errors. Use consistent formatting (e.g., 2-space indentation) in teams. For large files, consider streaming parsers.
- Keys must be strings in double quotes; single quotes are invalid.
- No trailing commas in objects or arrays.
- Use
nullfor missing values, notundefined. - Avoid circular references;
JSON.stringify()will fail. - Comments aren’t part of JSON-document separately.
Invalid example includes an unquoted key, single-quoted string, and trailing comma.
Arrays
Arrays are ordered lists inside [ ]. They may hold any value type-strings, numbers, objects, arrays, booleans, or null.
Objects
Objects are collections of name–value pairs inside { }. Values may be nested arbitrarily.
Accessing JSON in JavaScript
Parse with JSON.parse(); access with dot or bracket notation. Arrays are zero-indexed.
JSON Cheat Sheet
Quick rules: objects use curly braces, arrays use square brackets, strings are double-quoted, numbers are decimal with optional exponent, and values include true, false, or null. No comments, no trailing commas.
\" double quote\\ backslash\/ slash\b backspace\n newline\r carriage return\t tab\u1234 unicodeOur JSON Formatter/Validator Tool
Our free tool prettifies/minifies JSON, validates syntax in real-time, and exports clean output. Built with Monaco Editor for syntax highlighting, all client-side.
- Instant error highlighting.
- Copy/download options.
- Supports large files.
- Privacy-focused, no server uploads.
Further Reading
- ECMA-404: The JSON Data Interchange Standard - authoritative syntax.
- json.org - historical background and overview.