Chapters: JavaScript HTML & CSS JSON XML YAML ← Full TOC

← JSON Index

📋 JSON Syntax — Data types, syntax rules, and valid JSON structure

JSON (JavaScript Object Notation) is a lightweight text format for data interchange. It is human-readable and language-independent. JSON has exactly 6 data types and strict syntax rules — no comments, no trailing commas, only double-quoted strings.

📋 Reference

ItemSyntax / ValueDescription
string"Hello, World!"Double-quoted UTF-8 string — no single quotes, no unquoted
number42 / 3.14 / -7 / 1.5e10Integer or float — no Infinity, no NaN, no hex
booleantrue / falseLowercase only — not True or False
nullnullNull value — lowercase only
object{"key": value, "key2": value2}Unordered key-value pairs — keys MUST be strings
array[value1, value2, value3]Ordered list of values of any type
nestingobjects and arrays can nest infinitelyDeep nesting is valid but impacts performance
no trailing comma{"a":1,"b":2} ← correctTrailing commas are NOT valid JSON
no comments// and /* */ are NOT valid JSONJSON has no comment syntax
no undefinedundefined is NOT a valid JSON valueundefined is JavaScript-only
no functionsFunctions are NOT valid JSON valuesJSON is data only — no code
no NaN/InfinityNaN and Infinity are NOT valid JSONUse null instead
WhitespaceWhitespace (spaces, newlines) is ignoredJSON is whitespace-insensitive (outside strings)
UnicodeAll Unicode characters are valid in strings\uXXXX escape for control chars
Escape sequences\" \\ \/ \b \f \n \r \t \uXXXXValid JSON string escape sequences
Root elementAny JSON value can be the rootNot just objects — arrays, strings, numbers are valid root
Content-Typeapplication/jsonHTTP Content-Type header for JSON
File extension.jsonStandard file extension
BOMJSON MUST NOT have a byte order mark (BOM)UTF-8 without BOM is the standard
Max sizeNo defined limit — but large files impact performancePractical limit depends on parser/memory

💡 Example

{
  "_comment_note": "JSON has no comments — use this key trick for docs only",
  "student": {
    "id": 1001,
    "name": "Alice Smith",
    "email": "alice@mywebuniversity.com",
    "active": true,
    "gpa": 3.95,
    "graduation": null,
    "enrolled": "2024-09-01T08:00:00Z",
    "scores": [95, 87, 92, 98, 88],
    "address": {
      "street": "123 Learn Ave",
      "city": "Techville",
      "state": "CA",
      "zip": "90210",
      "country": "US"
    },
    "courses": [
      { "id": "CS101", "name": "Python",       "credits": 3, "grade": "A" },
      { "id": "CS201", "name": "JavaScript",   "credits": 3, "grade": "A-" },
      { "id": "CS301", "name": "Data Science", "credits": 4, "grade": "A+" }
    ],
    "tags": ["scholarship", "dean-list", "remote"],
    "metadata": {
      "created": "2024-08-15",
      "updated": "2026-06-25",
      "version": 3
    }
  }
}

/* Valid JSON types at root level: */
// Object:  { "key": "value" }
// Array:   [1, 2, 3]
// String:  "hello"
// Number:  42
// Boolean: true
// Null:    null

🏠 Index  |  JSON in JavaScript →