Guides › What Is JSON? Syntax Rules, Common Errors and Fixes, Formatting
What Is JSON? Syntax Rules, Common Errors and Fixes, Formatting
JSON (JavaScript Object Notation) is a lightweight text format for data. The name comes from JavaScript, but nearly every programming language supports it, and it is the most common format for web API responses, configuration files, IoT messages and logs. It is standardized as RFC 8259 and ECMA-404.
Structure and data types
{
"device": "sensor-01",
"online": true,
"temperature": 23.5,
"tags": ["line1", "zone-a"],
"location": { "lat": 37.5665, "lng": 126.978 },
"error": null
}
| Type | Example | Notes |
|---|---|---|
| string | "hello" | Double quotes only |
| number | 42, -3.14, 1e-6 | No int/float distinction; no leading zeros (012 is invalid) |
| boolean | true, false | Lowercase only |
| null | null | No value; lowercase only |
| object | {"key": value} | Key/value pairs; keys are strings |
| array | [1, 2, 3] | Ordered list; mixed types allowed |
Syntax rules (how JSON differs from JavaScript objects)
- Keys and strings must use double quotes. Single quotes and unquoted keys are errors.
- No trailing commas:
[1, 2, 3,]is invalid. - No comments: neither
//nor/* */(use JSON5, JSONC or YAML if you need them). - No
undefined,NaN,Infinity, functions or Date objects. Dates are usually ISO 8601 strings ("2026-11-29T09:00:00Z") or Unix timestamps. - Escape special characters in strings:
\"\\n\t\uXXXX. A raw newline inside a string is an error. - Encoding is UTF-8.
Common errors and fixes
| Invalid | Cause | Fix |
|---|---|---|
{name: "kim"} | Unquoted key | {"name": "kim"} |
{'name': 'kim'} | Single quotes | Use double quotes |
{"a": 1, "b": 2,} | Trailing comma | Remove the last comma |
{"a": 1 "b": 2} | Missing comma | Add a comma between members |
{"path": "C:\new"} | Backslash read as an escape | "C:\\new" |
{"v": NaN} | Not a JSON value | null or a string |
{"a": 1}} | Unbalanced braces | Count opening and closing brackets |
Reading error messages
- Unexpected token ... in JSON at position N: an unexpected character at position N. Check the comma or quote just before it.
- Unexpected end of JSON input: the data ends too early: a missing closing bracket, or an empty or truncated server response.
- Unexpected token < in JSON at position 0: you received HTML (usually an error or login page) instead of JSON. Check the API URL and response status.
- Bad control character in string literal: an unescaped newline or tab inside a string.
This site's JSON validator highlights the error position as soon as you paste.
Formatting (beautify) and minifying
Whitespace in JSON has no meaning, so the same data can take two forms.
- Formatted: indentation and line breaks for humans; for debugging, editing config files and code review.
- Minified: all unnecessary whitespace removed; for network transfer, storage and MQTT payloads.
// formatted
{
"id": 1,
"ok": true
}
// minified
{"id":1,"ok":true}
In JavaScript use JSON.stringify(obj, null, 2) to format and JSON.stringify(obj) to minify. In Python, json.dumps(obj, indent=2, ensure_ascii=False) keeps non-ASCII text readable instead of \uXXXX escapes.
Pitfalls
- Large numbers: JavaScript stores numbers as 64-bit floats, so integers above 253 (9,007,199,254,740,992) lose precision. Send 64-bit IDs as strings.
- Duplicate keys: the standard leaves behavior undefined; most parsers keep the last value.
- Security: never parse JSON with
eval(); always useJSON.parse(). - Tree view: for large, deeply nested JSON, a tree viewer shows the structure much faster.
JSON vs XML vs CSV
| Item | JSON | XML | CSV |
|---|---|---|---|
| Structure | Nested objects and arrays | Nested tags and attributes | Table (rows and columns) |
| Size | Small | Large (closing tags) | Smallest |
| Types | String, number, boolean, null | All text (types via schema) | All text |
| Typical use | Web APIs, config, IoT | Documents, enterprise systems, SOAP | Spreadsheets, analysis, logs |
For format conversion and test data see the CSV, XML and test data guide.
Try it now: JSON Validator (runs in your browser, no data is sent anywhere)