What is JSON?
JSON (JavaScript Object Notation) is a lightweight text format for storing and exchanging structured data. It writes data as key/value pairs inside curly braces and lists inside square brackets, so people can read it and every programming language can parse it.
{
"name": "Ada",
"age": 36,
"skills": ["math", "poetry"]
}Try it: build a JSON object
Change a key, a type or a value and watch the JSON and the tree update. Notice which values get quotes and which do not.
Your JSON
{
"name": "Ada Lovelace",
"age": 36,
"isAdmin": true,
"skills": [
"math",
"poetry",
1843
],
"address": {
"city": "London",
"zip": "W1"
},
"nickname": null
}Tree view: what a program sees
- object6 keys
namestring"Ada Lovelace"agenumber36isAdminbooleantrueskillsarray3 items0string"math"1string"poetry"2number1843
addressobject2 keyscitystring"London"zipstring"W1"
nicknamenullnull
JSON syntax and the 6 data types
Data is written as "key": value pairs separated by commas. Objects use curly braces, arrays use square brackets, and every value is one of these six types.
| Type | Example | Rule |
|---|---|---|
| String | "Ada" | Text in double quotes |
| Number | 36, -2.5, 1e3 | No quotes; no NaN or Infinity |
| Boolean | true, false | Lowercase, no quotes |
| Null | null | An empty or unknown value |
| Array | ["math", 1843] | An ordered list in square brackets |
| Object | { "city": "London" } | Key/value pairs in curly braces |
Spot the JSON syntax error
Most invalid JSON comes from six mistakes. Pick one to see the error your browser reports and the fix.
Broken
{ 'name': 'Ada' }Your browser's JSON.parse says: …
Fixed
{ "name": "Ada" }JSON strings and keys must use double quotes. Single quotes are fine in JavaScript, not in JSON.
Got a file that will not parse? Paste it into the JSON formatter and validator to find the exact line.
Where JSON is used
- Web APIs
- When an app loads your feed or weather, the server almost always answers with JSON.
- Config files
- package.json, tsconfig.json and VS Code's settings.json all store settings as JSON.
- Databases
- MongoDB stores JSON-like documents, and PostgreSQL and MySQL have JSON column types.
- Saving app data
- Browsers store app state in localStorage as JSON strings, and many games save progress the same way.
JSON vs XML vs YAML
Here is the same person written in all three formats.
JSON
{
"name": "Ada",
"skills": ["math", "poetry"]
}XML
<person>
<name>Ada</name>
<skills>
<skill>math</skill>
<skill>poetry</skill>
</skills>
</person>YAML
name: Ada
skills:
- math
- poetry| Feature | JSON | XML | YAML |
|---|---|---|---|
| Best for | Web APIs and data exchange | Documents, older enterprise and SOAP systems | Config files people edit by hand |
| Readability | Good | Wordy: every value needs an opening and closing tag | Best: no brackets or quotes needed |
| Comments | No | Yes | Yes |
| Data types | String, number, boolean, null, array, object | Everything is text unless a schema says otherwise | Same as JSON, plus dates and more |
| Parsing in JavaScript | Built in: JSON.parse | Needs DOMParser or a library | Needs a library |
How to write valid JSON
- Start with curly braces. Wrap the whole object in { and }. Use [ and ] instead if the top level is a list.
- Write each key in double quotes. Type the key as a string, a colon, then the value: "name": "Ada".
- Pick the right value type. Quote strings, leave numbers, true, false and null unquoted, and nest arrays or objects as needed.
- Separate pairs with commas. Put a comma between items, but never after the last one.
- Validate it. Paste it into a JSON validator, or run JSON.parse, to catch a stray quote or comma before you ship it.
JSON quiz: test yourself
0 of 5 answered
Frequently asked questions
What is JSON in simple terms?
JSON (JavaScript Object Notation) is a lightweight text format for storing and sending structured data. It writes data as key/value pairs in curly braces and lists in square brackets, so both people and programs can read it.
What is JSON used for?
JSON is mostly used to send data between a server and a web or mobile app through APIs. It is also used for config files such as package.json, for storing documents in databases, and for saving app data.
Is JSON a programming language?
No. JSON is a data format, not a language. It cannot run code, loop or make decisions. It only describes data, which a program in any language can read and write.
What is the difference between JSON and a JavaScript object?
A JavaScript object is live data in memory and can hold functions, undefined and single-quoted strings. JSON is text with stricter rules: double-quoted keys and strings, no functions, no comments. JSON.stringify and JSON.parse convert between them.
Is JSON better than XML?
For web APIs, usually yes: JSON is shorter, faster to parse and maps straight onto objects in code. XML is still used for documents, older enterprise systems and formats that need attributes, namespaces or comments.
How do I open a JSON file?
A .json file is plain text, so any text editor opens it, including Notepad, TextEdit or VS Code. Browsers like Firefox and Chrome also display JSON, and a JSON formatter makes long files easier to read.