DevLab
JSON

JSON Format Explained: Structure, Syntax, and Common Errors

Understand JSON syntax, valid vs invalid values, parsing in JavaScript and other languages, the difference from JSON5, and how to validate JSON with schemas.

What JSON Is

JSON (JavaScript Object Notation) is a lightweight text format for representing structured data. It was derived from JavaScript object literal syntax, but it is language-independent — every mainstream programming language has a JSON parser in its standard library or as a first-party package.

JSON is used for API responses, configuration files, data exchange between services, and local storage. Its success comes from being human-readable, minimal, and unambiguous.

The Six Value Types

JSON supports exactly six types — no more, no less:

  • String: "hello world" — always double-quoted, never single quotes
  • Number: 42, 3.14, -7, 1.5e10 — no special integer vs float distinction
  • Boolean: true or false — lowercase, no quotes
  • Null: null — represents absence of value
  • Array: [1, "two", true, null] — ordered list, heterogeneous types allowed
  • Object: {"key": "value"} — unordered map of string keys to any value

Notable omissions: no undefined, no NaN, no Infinity, no Date type, no comments. These are by design — they make parsing deterministic across implementations.

Syntax Rules

JSON is strict. Any deviation produces a parse error.

  • Object keys must be strings: {"name": "Alice"} is valid; {name: "Alice"} is not
  • Strings must use double quotes: "hello" is valid; 'hello' is not
  • No trailing commas: [1, 2, 3,] is invalid; [1, 2, 3] is correct
  • No comments: // comment or /* comment */ cause parse errors
  • Numbers have limits: Integers beyond 2^53 − 1 lose precision in JavaScript's JSON.parse(); use strings for large IDs

Common Parse Errors

// INVALID: single quotes
{ 'name': 'Alice' }

// INVALID: trailing comma
{ "items": [1, 2, 3,] }

// INVALID: comment
{ "version": 1 // app version }

// INVALID: undefined value
{ "result": undefined }

// INVALID: unquoted key
{ name: "Alice" }

// VALID equivalents
{ "name": "Alice" }
{ "items": [1, 2, 3] }
{ "version": 1 }
{ "result": null }
{ "name": "Alice" }

Parsing in Different Languages

// JavaScript
const obj = JSON.parse('{"name":"Alice","age":30}')
const str = JSON.stringify(obj, null, 2) // 2-space indent

// Python
import json
obj = json.loads('{"name":"Alice","age":30}')
str = json.dumps(obj, indent=2)

// Go
import "encoding/json"
type Person struct { Name string `json:"name"`; Age int `json:"age"` }
var p Person
json.Unmarshal([]byte(input), &p)
out, _ := json.MarshalIndent(p, "", "  ")

// Java (Jackson)
ObjectMapper mapper = new ObjectMapper()
Person p = mapper.readValue(input, Person.class)
String out = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(p)

JSON vs JSON5

JSON5 is a superset of JSON designed to be more human-friendly. It adds features that JSON deliberately excludes:

  • Single-quoted strings: 'hello'
  • Trailing commas: [1, 2, 3,]
  • Comments: // line and /* block */
  • Unquoted keys: {name: "Alice"}
  • Multi-line strings via backslash continuation
  • Numeric values like Infinity, -Infinity, NaN, and hexadecimal literals

JSON5 is great for configuration files where humans edit it directly (e.g., tsconfig.json with comments is technically JSON5-compatible). Never use it for API payloads — most HTTP clients and servers expect strict JSON.

JSON Schema: Validating Structure

JSON Schema is a separate standard for describing what a valid JSON document should look like. A schema is itself a JSON document:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["id", "name"],
  "properties": {
    "id":    { "type": "integer" },
    "name":  { "type": "string", "minLength": 1 },
    "email": { "type": "string", "format": "email" },
    "tags":  { "type": "array", "items": { "type": "string" } }
  },
  "additionalProperties": false
}

Validators (Ajv in JavaScript, jsonschema in Python) check a document against a schema at runtime. This is how API libraries like Fastify, tRPC, and OpenAPI validate request bodies without writing manual validation logic.

Working with Large Numbers

JavaScript's JSON.parse() converts all numbers to IEEE 754 doubles. Integers above 2^53 − 1 (9,007,199,254,740,991) lose precision silently. This is a real problem with database IDs from systems like Snowflake (Twitter) or PostgreSQL bigint.

// Problem: precision lost
JSON.parse('{"id": 9999999999999999}')
// → { id: 10000000000000000 }  ← wrong!

// Solution: send large integers as strings
JSON.parse('{"id": "9999999999999999"}')
// → { id: "9999999999999999" }  ← correct

Performance Tips

  • Minimize whitespace in transit: Use JSON.stringify(data) (no indent) for API responses; only use pretty-print for debugging
  • Parse once, reference many times: Do not call JSON.parse() inside a loop if the input does not change
  • For very large payloads: Use streaming parsers (JSONStream in Node.js, ijson in Python) instead of parsing the entire document into memory
  • For repeated serialization: Libraries like fast-json-stringify (Node.js) use a schema to serialize 2-5x faster than JSON.stringify()

Frequently Asked Questions

What does JSON stand for?

JSON stands for JavaScript Object Notation. Despite the name, JSON is language-independent and supported by virtually every programming language. It was derived from JavaScript object literal syntax but is now the most widely used data interchange format on the web.

What data types does JSON support?

JSON supports six data types: strings (double-quoted), numbers (integer or floating-point), booleans (true/false), null, objects (key-value pairs in curly braces), and arrays (ordered lists in square brackets). It does not support dates, undefined, functions, comments, or binary data.

What is the difference between JSON and a JavaScript object?

JSON is a text-based data format with strict syntax rules: keys must be double-quoted strings, no trailing commas, no comments, no undefined values. JavaScript objects are in-memory data structures that allow single-quoted keys, computed properties, methods, symbols, and other features JSON cannot represent.

Why do APIs use JSON instead of XML?

JSON is significantly more compact than XML (no closing tags), faster to parse, maps directly to native data structures in most languages, and is the default format for JavaScript fetch/XMLHttpRequest. XML offers namespaces and schemas that JSON lacks, but for most web APIs the simplicity and performance of JSON wins.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines