How to write and validate JSON
JSON (JavaScript Object Notation) is the most common format for sending data between programs: web APIs return it, configuration files use it, and databases store it. It is small and readable, but it is also strict. A single misplaced comma makes a whole document invalid.
This guide covers JSON's building blocks, the rules that trip people up, and how to check that a document is valid.
The six JSON data types
Every JSON value is one of these:
| Type | Example | Notes |
|---|---|---|
| Object | {"name": "Ada"} | Unordered key/value pairs. Keys must be strings in double quotes. |
| Array | [1, 2, 3] | An ordered list. Items can be of mixed types. |
| String | "hello" | Always double quotes. Special characters are escaped with a backslash. |
| Number | 42, -3.5, 1e6 | No leading zeros, no + sign, no NaN or Infinity. |
| Boolean | true, false | Lowercase only. |
| Null | null | Lowercase. Means "no value". |
Objects and arrays can be nested inside each other to any depth. That is how JSON represents rich data like an order with a customer, several items and a shipping address.
{
"orderId": "A-1042",
"paid": true,
"total": 59.9,
"coupon": null,
"customer": { "name": "Ada Lovelace", "email": "ada@example.com" },
"items": [
{ "sku": "BK-001", "qty": 2 },
{ "sku": "PN-220", "qty": 1 }
]
}The strict rules
JSON is defined by two matching standards, RFC 8259 and ECMA-404. It looks like JavaScript, but it is much stricter. These rules cause most errors:
- Double quotes only.
'single quotes'are not allowed, for strings or for keys. - Every key is quoted.
{name: "Ada"}is valid JavaScript but invalid JSON. - No trailing commas.
[1, 2, 3,]is invalid. - No comments. Neither
//nor/* */is part of JSON. - Escape special characters in strings:
\"for a quote,\\for a backslash,\nfor a new line,\tfor a tab andéfor any Unicode character by code. - No
undefined, functions or dates. Dates are usually written as strings, for example"2026-11-22T10:30:00Z".
See Common JSON syntax errors for broken examples of each rule and how to fix them.
Tips for designing good JSON
- Pick one naming style and stick to it, such as
camelCaseorsnake_case. - Use ISO 8601 for dates and times and include a time zone or a
Zfor UTC. - Be careful with very large integers. JavaScript can only represent whole numbers exactly up to 253 − 1 (9,007,199,254,740,991). IDs larger than that should be sent as strings.
- Represent money carefully. Floating-point numbers can't store some decimals exactly, so many APIs send amounts in minor units (for example
5990cents) or as strings. - Prefer arrays of objects to parallel arrays.
[{"name":"a","qty":1}]is easier to read and harder to break than two arrays that must stay in sync.
Two kinds of validation
1. Syntax validation: is it JSON at all?
A parser reads the text and either produces data or reports the first error it finds. This answers "will my program be able to read this?" Every language has a parser built in:
- In a browser console:
JSON.parse(text) - On the command line with Python:
python3 -m json.tool data.json - With the
jqtool:jq . data.json - In Dev Studio: paste into the JSON Viewer. It shows the line, column and reason for the first error.
2. Schema validation: is it the right JSON?
Valid syntax doesn't mean the data makes sense. {"qty": "lots"} is valid JSON, but your program probably expects a number. JSON Schema is a separate standard for describing the shape you expect, and libraries in most languages can check data against it. A small schema for an order item looks like this:
{
"type": "object",
"required": ["sku", "qty"],
"properties": {
"sku": { "type": "string" },
"qty": { "type": "integer", "minimum": 1 }
},
"additionalProperties": false
}Dev Studio's JSON Viewer checks syntax, not schemas. Use it to confirm a document parses and to explore its structure, then use a JSON Schema library in your code or tests to enforce the shape.
A quick validation workflow
- Paste or load the JSON into the JSON Viewer's Text tab.
- If the status bar shows an error, jump to the highlighted line and fix it. Repeat until it says Valid JSON.
- Press Format to get consistent indentation, which makes structural mistakes obvious.
- Switch to Viewer to confirm keys and nesting are where you expect.