DevToolbox
Developer Guide

JSON Syntax Guide

Everything you need to know about JSON — data types, formatting rules, common mistakes, comparison with YAML and XML, and JavaScript usage examples.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and transmitting structured data. Despite its name, JSON is language-independent and is supported natively by every major programming language.

JSON is used everywhere: REST APIs, configuration files, databases (MongoDB, Firestore), environment settings, and data exchange between services. If you have worked with an API, you have worked with JSON.

Basic JSON document
{
  "name": "Alice",
  "age": 30,
  "active": true,
  "tags": ["developer", "designer"],
  "address": {
    "city": "Tokyo",
    "country": "Japan"
  },
  "nickname": null
}

JSON Data Types

JSON supports exactly six data types. Every value in a JSON document must be one of these.

String
"Hello, world!"

Must use double quotes. Supports escape sequences: \" \n \t \\

Number
42 or 3.14 or -7 or 1e10

No distinction between integer and float. No NaN or Infinity.

Boolean
true or false

Lowercase only. Uppercase TRUE is not valid JSON.

Null
null

Represents the intentional absence of a value. Lowercase only.

Object
{ "key": value }

Unordered key-value pairs. Keys must be double-quoted strings.

Array
[value1, value2]

Ordered list of values. Can mix types: [1, "two", true, null].

All six types in one object
{
  "string":  "hello",
  "number":  42,
  "boolean": true,
  "null":    null,
  "object":  { "nested": "value" },
  "array":   [1, 2, 3]
}

JSON Syntax Rules

JSON has strict syntax requirements. Even a single violation causes a parse error.

Keys must use double quotes
Invalid
{ name: "Alice" }      // Single quotes or unquoted
Valid
{ "name": "Alice" }    // Double quotes required
Strings must use double quotes
Invalid
{ "name": 'Alice' }    // Single-quoted string
Valid
{ "name": "Alice" }   // Double-quoted string
No trailing commas
Invalid
{ "a": 1, "b": 2, }   // Trailing comma after last item
Valid
{ "a": 1, "b": 2 }   // No trailing comma
No comments
Invalid
{ "a": 1 // this is a  // Comments not allowed
           comment }
Valid
{ "a": 1 }            // Use a separate docs file
Special characters must be escaped
Invalid
{ "msg": "say "hi"" } // Unescaped quotes
Valid
{ "msg": "say \"hi\"" } // Escaped with backslash

Top 5 Common JSON Errors

These are the mistakes that cause most JSON parse failures in practice.

01Trailing comma

Adding a comma after the last item in an object or array. JavaScript allows this; JSON does not.

Before (invalid)
[
  "apple",
  "banana",   ← trailing comma
]
After (valid)
[
  "apple",
  "banana"
]
02Single-quoted strings

Using single quotes instead of double quotes around strings or keys.

Before (invalid)
{ 'name': 'Alice' }
After (valid)
{ "name": "Alice" }
03Unquoted keys

Object keys that look like JavaScript identifiers work in JS objects but are invalid in JSON.

Before (invalid)
{ name: "Alice", age: 30 }
After (valid)
{ "name": "Alice", "age": 30 }
04Undefined or NaN values

JavaScript's undefined, NaN, and Infinity are not valid JSON values.

Before (invalid)
{ "value": undefined,
  "ratio": NaN }
After (valid)
{ "value": null,
  "ratio": 0 }
05Unescaped special characters

Newlines, tabs, and quotes inside strings must be escaped with a backslash.

Before (invalid)
{ "note": "line one
line two" }
After (valid)
{ "note": "line one\nline two" }

JSON vs YAML vs XML

Choosing a data format depends on your context — API responses, config files, or legacy systems each have different trade-offs.

FeatureJSONYAMLXML
Human readabilityGoodExcellentPoor
Comments supportedNoYesYes
Trailing commasNot allowedN/AN/A
Data types6 nativeRicher (dates, etc.)Strings only (without schema)
VerbosityLowVery lowHigh
Browser supportNativeNeeds libraryNeeds library
Best forAPIs, databasesConfig filesEnterprise / legacy

JSON in JavaScript

JavaScript provides two built-in methods for working with JSON.

JSON.parse() — string to object

const jsonString = '{"name":"Alice","age":30}';
const user = JSON.parse(jsonString);

console.log(user.name); // "Alice"
console.log(user.age);  // 30

// Always wrap in try/catch for user input
try {
  const data = JSON.parse(untrustedInput);
} catch (err) {
  console.error("Invalid JSON:", err.message);
}

JSON.stringify() — object to string

const user = { name: "Alice", age: 30, active: true };

// Compact (for APIs)
const compact = JSON.stringify(user);
// → '{"name":"Alice","age":30,"active":true}'

// Pretty-printed (for logging)
const pretty = JSON.stringify(user, null, 2);
// → {
//     "name": "Alice",
//     "age": 30,
//     "active": true
//   }

// Exclude undefined values automatically
const obj = { a: 1, b: undefined };
JSON.stringify(obj); // → '{"a":1}'

JSON Best Practices

Use consistent key naming

Choose one convention and stick to it. camelCase ("firstName") is standard for JavaScript APIs; snake_case ("first_name") is common in Python and databases.

Avoid deep nesting

More than 3–4 levels of nesting makes JSON hard to read and query. Flatten structures or use references (IDs) to related objects.

Use arrays for lists

Prefer arrays over numbered object keys. {"0": "a", "1": "b"} is a code smell — use ["a", "b"] instead.

Be explicit about null

Use null for intentionally missing values, and omit the key entirely for optional fields that are not applicable. Document the difference.

Validate on the server

Never trust JSON from external sources without validation. Use a schema validator (Zod, AJV, Joi) to enforce structure and types.

Keep API responses flat

Flat JSON is faster to parse and easier to map to UI components. Use query parameters to request only the fields you need.

Put JSON knowledge to work

Format, validate, and convert JSON right in your browser — no setup required.

JSON FAQ

Answers to common questions about JSON syntax, usage, and tooling.

What is JSON?+

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format. It is easy for humans to read and write, and easy for machines to parse and generate. JSON is derived from JavaScript object literal syntax but is language-independent and supported by virtually every programming language.

Does JSON support comments?+

No. The JSON specification does not allow comments. If you need to add comments to configuration files, consider using JSONC (JSON with Comments) or YAML instead. Parsers that accept comments (like VS Code's JSON parser) are using a superset of the JSON standard.

What is the difference between JSON.parse() and JSON.stringify()?+

JSON.parse() converts a JSON string into a JavaScript object or value. JSON.stringify() converts a JavaScript object or value into a JSON string. Use parse when receiving JSON (e.g. from an API), and stringify when sending data or storing it as text.

Can JSON keys be numbers?+

No. In JSON, all object keys must be strings enclosed in double quotes. Numbers, booleans, and unquoted identifiers are not valid JSON keys. For example, {"1": "value"} is valid JSON, but {1: "value"} is not.

What is the maximum depth for nested JSON?+

The JSON specification does not define a maximum nesting depth, but parsers and languages often impose limits. In practice, deeply nested JSON (more than 5–10 levels) is a design smell. Flat or shallow structures are easier to query, validate, and maintain.