JSON Syntax

JSON Syntax Rules

The JSON syntax is derived directly from JavaScript object notation syntax.

However, the JSON format is text-only, meaning it has strict structural rules to prevent data corruption.


Core Syntax Rules

JSON data is always written as name/value pairs.

Different data pairs are strictly separated by commas.

Curly braces {} are used exclusively to hold JSON objects.

Square brackets [] are used exclusively to hold JSON arrays.


JSON Names Require Double Quotes

In standard JavaScript, object keys (names) can be written without quotes.

In strict JSON, all names must be wrapped securely in double quotes!

Single quotes will cause the parser to instantly fail and throw a syntax error.

Correct JSON Syntax Example:

// The key MUST be in double quotes. The string value MUST be in double quotes.
let validJSON = '{"firstName":"John"}';

// Parsing the valid JSON string let obj = JSON.parse(validJSON); console.log(obj.firstName);


JSON Values

JSON values must be specific valid data types, such as strings, numbers, objects, arrays, booleans, or null.

Unlike JavaScript, JSON values cannot be functions, dates, or undefined variables.

This restriction guarantees the data can be safely transferred between completely different server languages.


Exercise 1 of 1

?

In strict JSON syntax, how must keys (names) be written?