ورقة مرجعية JSON
مرجع بناء جملة JSON: أنواع البيانات، أحرف الهروب، اختلافات JSON5، الأخطاء الشائعة وتنسيقات التاريخ.
Data types
| Type | Example | Notes |
|---|---|---|
| string | "hello" | Double quotes only. UTF-8. |
| number | 42 or 3.14 or -1.2e10 | No NaN, no Infinity. |
| boolean | true or false | Lowercase only. |
| null | null | Lowercase. 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
| Sequence | Character |
|---|---|
\" | Double quote |
\\ | Backslash |
\/ | Forward slash (optional escape) |
\n | Newline |
\r | Carriage return |
\t | Tab |
\b | Backspace |
\f | Form feed |
\uXXXX | Unicode 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
| Feature | JSON | JSONC | JSON5 | YAML |
|---|---|---|---|---|
| Comments | ❌ | ✅ // and /* */ | ✅ | ✅ # |
| Trailing commas | ❌ | ✅ | ✅ | N/A |
| Unquoted keys | ❌ | ❌ | ✅ | ✅ |
| Single quotes | ❌ | ❌ | ✅ | ✅ |
| Multi-line strings | ❌ | ❌ | ✅ | ✅ |
| Hex / Inf / NaN | ❌ | ❌ | ✅ | Limited |
| Native browser support | ✅ | ❌ | ❌ | ❌ |
| Use case | APIs, storage | VS Code configs | Human-edited configs | CI/CD, K8s |
Date conventions
| Format | Example | Notes |
|---|---|---|
| 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 seconds | 1746557025 | Compact, no timezone ambiguity. |
| Unix milliseconds | 1746557025000 | JavaScript Date.now() format. |
Common gotchas
- • JSON.parse(undefined) throws. Always JSON.parse(string), where string is non-undefined.
- • JSON.stringify(undefined) === undefined.
undefinedvalues 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.
أدوات ذات صلة
- JSON Formatter
تنسيق وتجميل وتصغير والتحقق من صحة JSON في متصفحك.
- مولد رمز QR
توليد رموز QR للروابط والنصوص وشبكات Wi-Fi. تنزيل بصيغة PNG.
- مولد كلمات المرور
إنشاء كلمات مرور قوية وعشوائية بطول وأنواع حروف مخصصة.
- Base64 Encoder / Decoder
ترميز النص إلى Base64 أو فك ترميزه.
- URL Encoder / Decoder
ترميز أو فك ترميز URLs ومعاملات الاستعلام.