The JSON format
Learn JSON syntax and which data types it supports.
A text-based data format
Applications often use a text-based format to store data and send it between systems. This format uses key-value pairs and is easy for both humans and machines to read.
{"name": "Alex", "age": 30}Values are wrapped in double quotes, and so are keys. The format is based on JavaScript object literal syntax but stricter: single quotes, trailing commas and comments are not allowed.
Supported data types
This format supports six core types: string, number, boolean, null, array and object. Strings are always double-quoted; numbers are written without quotes.
{"active": true, "count": 5, "tags": ["a", "b"]}Dates, functions and undefined are not supported directly; you must convert them to a string or number first.
Exercise Write a valid record string
Assign a valid format text to the product variable with a name key of "Pen" and a price key of 25. Keys must be double-quoted, the number unquoted.
Show example solution
Keys are double-quoted, the number is unquoted.
Example solution:
const product = '{"name": "Pen", "price": 25}';
Test yourself
-
Which is a valid record?
- {"name": "Alex"}
- {'name': 'Alex'}
- {name: "Alex"}
- <name>Alex</name>
Show answer
{"name": "Alex"} — Correct! The double-quoted form is valid.
-
Match each type to its example
- string
- "hello"
- number
- 42
- boolean
- true
- empty
- null
-
Write the empty value
const data = '{"city": ____}';Show answer
const data = '{"city": null}';