Get started with JavaScript Object Notation
JavaScript Object Notation (JSON) is a lightweight data interchange format that's easy for both humans and machines to read and write. JSON is widely used for transmitting data between a server and a web application, as well as for storing and exchanging data in various applications.
What is JSON?
JSON is a text-based format that represents structured data using key-value pairs. It's language-independent, meaning it can be used with any programming language. JSON is often used in web development for Application Programming Interfaces (APIs) to send and receive data between a client and a server.
JSON file syntax
The JSON file syntax is simple and easy to understand. JSON data is represented as a collection of key-value pairs, where keys are strings and values can be strings, numbers, Booleans, arrays, or objects.
The basic structure of JSON consists of:
- Objects: Objects are collections of key-value pairs that are enclosed in curly braces
{}. Each key is a string, followed by a colon and the corresponding value. Multiple key-value pairs are separated by commas. - Arrays: Arrays are ordered lists of values that are enclosed in square brackets
[]. Values can be of any type, including objects and other arrays. Elements in an array are separated by commas. - Values: Values can be strings, numbers, Booleans, arrays, or objects. Strings are enclosed in double quotes, numbers are written without quotes, and Booleans are represented as true or false. A null value is represented by the keyword null.
- Whitespace: JSON allows for whitespace (spaces, tabs, and newlines) to be used for readability. However, whitespace isn't significant in JSON and can be ignored by parsers.
- Comments: JSON doesn't support comments.
- Encoding: JSON is encoded in UTF-8, which means it can represent any Unicode character. This allows for the inclusion of special characters and symbols in JSON data.
JSON's basic data types are:
- Number: A signed decimal number that may contain a fractional part and may use exponential E notation but can't include non-numbers. The format makes no distinction between integer and floating-point.
- String: A sequence of zero or more Unicode characters. Strings are delimited with double quotation marks and support a backslash escaping syntax.
- Boolean: Either of the values
trueorfalse. - Array: An ordered list of zero or more elements, each of which may be of any type. Arrays use square bracket notation
[]with comma-separated elements. - Object: A collection of
name:valuepairs where the names are strings. Objects are delimited with curly brackets{}and use commas to separate each pair, while within each pair, the colon:character separates thenamefrom itsvalue. Values can be of any supported type, including other objects or arrays. - null: An empty value, using the word
null.
JSON samples
Since JSON is human-readable, reviewing some examples can help you understand its structure and syntax.
The following example demonstrates the syntax for a JSON object that contains various data types:
{
"name": "John Doe",
"age": 30,
"isEmployed": true,
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
}
}
This example shows an object with several key-value pairs, including a nested object for the address. The keys are strings, and the values include a string, a number, a Boolean, and another object.
The following example demonstrates the syntax for a JSON array:
[
"apple",
"banana",
"cherry"
]
An array is represented by square brackets and contains a list of values. In this case, the array contains three string values. Each value is separated by a comma.
Summary
JSON is a lightweight, text-based data interchange format that is easy to read and write. It uses a simple syntax based on key-value pairs, making it suitable for representing structured data. JSON is widely used in web development and APIs for transmitting data between clients and servers.