Stax

JSON चीट शीट

JSON syntax संदर्भ: data types, escape characters, JSON5 अंतर, common pitfalls और date conventions।

Data types

TypeExampleNotes
string"hello"Double quotes only. UTF-8.
number42 or 3.14 or -1.2e10No NaN, no Infinity.
booleantrue or falseLowercase only.
nullnullLowercase. Means "explicitly absent."
object{ "key": "value" }Keys MUST be double-quoted strings.
array[1, 2, 3]Ordered list, mixed types allowed.

Escape sequences inside strings

SequenceCharacter
\"Double quote
\\Backslash
\/Forward slash (optional escape)
\nNewline
\rCarriage return
\tTab
\bBackspace
\fForm feed
\uXXXXUnicode codepoint (4 hex digits)

Valid vs invalid

✓ Valid

{
  "name": "Alice",
  "age": 30,
  "tags": ["admin", "user"],
  "deleted_at": null
}

✗ Invalid

{
  name: 'Alice',     // unquoted key, single quote
  age: NaN,          // NaN not allowed
  tags: ['admin',],  // trailing comma
  // deleted_at      // comment not allowed
}

JSON vs JSON5 vs JSONC vs YAML

FeatureJSONJSONCJSON5YAML
Comments✅ // and /* */✅ #
Trailing commasN/A
Unquoted keys
Single quotes
Multi-line strings
Hex / Inf / NaNLimited
Native browser support
Use caseAPIs, storageVS Code configsHuman-edited configsCI/CD, K8s

Date conventions

FormatExampleNotes
ISO 8601 (UTC)"2026-05-06T14:23:45Z"Standard for APIs. Z = UTC.
ISO 8601 (offset)"2026-05-06T14:23:45+05:30"With timezone offset.
Unix seconds1746557025Compact, no timezone ambiguity.
Unix milliseconds1746557025000JavaScript Date.now() format.

Common gotchas

  • JSON.parse(undefined) throws. Always JSON.parse(string), where string is non-undefined.
  • JSON.stringify(undefined) === undefined. undefined values in objects are silently dropped.
  • Numbers lose precision beyond 2^53 (~9 quadrillion). Use string representation for IDs, big integers, or amounts.
  • No circular references — JSON.stringify throws on circular objects. Break the cycle or use a library like flatted.
  • Sorting keys isn't guaranteed — most parsers preserve insertion order, but the spec doesn't require it. Don't rely on key ordering for security or comparison.
  • Date objects become ISO strings in JSON.stringify. To rehydrate, use a reviver: JSON.parse(s, (k, v) => /^\d{4}-\d{2}-\d{2}/.test(v) ? new Date(v) : v).

Format and validate JSON with our JSON Formatter, or browse JSON visually in the JSON Tree Viewer.

The JSON spec in one paragraph

JSON (JavaScript Object Notation) is a text format for structured data. Six data types: string, number, boolean, null, object (key-value pairs), and array (ordered list). Strings use double quotes only. Object keys must be quoted. No comments, no trailing commas, no undefined, no NaN, no Infinity. That's the entire spec.

Use cases & flavor selection

HTTP APIs: strict JSON. VS Code / TypeScript config: JSONC. Configuration files for human editing: JSON5 or YAML. Storage formats: strict JSON or BSON (binary). When in doubt, default to strict JSON — it's the most universally parseable.

अक्सर पूछे जाने वाले प्रश्न

What's the difference between JSON, JSON5, and JSONC?
JSON is the strict spec — no comments, no trailing commas, no unquoted keys, double quotes only. JSONC (JSON with Comments) adds // and /* */ comments — Microsoft uses it for tsconfig.json, .vscode/settings.json. JSON5 is more permissive — adds comments, trailing commas, single quotes, unquoted keys, hex numbers. None of these are interchangeable with strict JSON in production APIs — the API spec dictates which flavor to use.
Why do trailing commas break JSON?
Strict JSON forbids trailing commas after the last element of arrays and objects. JavaScript allows them, so developers paste JS-like data and get parse errors. JSON5 and JSONC allow trailing commas. If you need JSON output that other systems will consume, never include them. JSON.stringify() in JavaScript correctly omits trailing commas.
Can JSON have comments?
Strict JSON cannot. Comments were intentionally excluded by Douglas Crockford (JSON's creator) to keep the format minimal. If you need comments, use JSON5 or JSONC. As a workaround in strict JSON, some teams use a special key like '_comment' to embed notes — though this clutters the data.
How do I represent dates in JSON?
Use ISO 8601 strings: '2026-05-06T14:23:45Z' (UTC) or '2026-05-06T14:23:45+05:30' (with offset). This is the de facto standard in 95% of APIs. Alternatives: Unix timestamps as integers (1746557025) — concise but less human-readable. Avoid local-format strings ('5/6/2026') — they're ambiguous between US and rest-of-world conventions.
Why are some characters showing as \u00XX in JSON?
JSON escape sequences. Characters outside ASCII or with special meaning (", \\, /) are escaped as \u followed by 4 hex digits. JSON.stringify() escapes these by default for safety. To get human-readable Unicode, parse and re-stringify in your application code, or use JSON.stringify(obj, null, 2) which preserves Unicode in modern engines.

संबंधित टूल्स