JSON Beginners Guide - Syntax, Data Types, and Common Errors
JSON (JavaScript Object Notation) is the most common data format for APIs and web applications. This guide covers the essentials.
What is JSON?
JSON is a lightweight, text-based format for storing and transporting data. It's human-readable and machine-parseable, making it the de facto standard for API responses.
JSON Syntax Rules
- Data is in key-value pairs:
"name": "ToolNest" - Keys must be in double quotes (single quotes not allowed)
- Values can be strings, numbers, booleans, null, arrays, or objects
- Items are separated by commas
- Objects use
{}curly braces, arrays use[]square brackets - No trailing commas allowed
- No comments allowed in strict JSON
JSON Data Types
| Type | Example | Description |
|---|---|---|
| String | "hello" |
Text, must use double quotes |
| Number | 42 or 3.14 |
Integer or float, no quotes |
| Boolean | true / false |
No quotes |
| Null | null |
Represents no value |
| Array | [1, 2, 3] |
Ordered list |
| Object | {"key": "value"} |
Key-value collection |
Example JSON
{
"name": "ToolNest",
"version": 1,
"tools": ["word-counter", "json-formatter"],
"active": true,
"description": null
}
JSON vs JavaScript Object
JSON looks like a JavaScript object, but there are differences:
| Feature | JSON | JavaScript Object |
|---|---|---|
| Keys | Must be double-quoted | Can be unquoted |
| Comments | Not allowed | Allowed |
| Trailing comma | Not allowed | Allowed |
| Functions | Not allowed | Allowed as values |
| Undefined | Not allowed | Allowed |
Common JSON Errors
- Single quotes:
'key': 'value'→ Use double quotes instead - Trailing comma:
{"a": 1,}→ Remove the last comma - Unquoted keys:
{name: "John"}→ Use{"name": "John"} - Comments:
// comment→ JSON doesn't support comments - Special characters: Use Unicode escapes for control characters
Working with JSON in JavaScript
// Parse JSON string to object
const obj = JSON.parse('{"name":"ToolNest"}');
// Convert object to JSON string
const str = JSON.stringify(obj, null, 2); // 2 = pretty-print with 2 spaces
// Validate JSON
try {
JSON.parse(input);
console.log("Valid JSON");
} catch (e) {
console.log("Invalid: " + e.message);
}
Use ToolNest JSON Formatter to beautify, minify, and validate your JSON data.