JSON
JSON stands for JavaScript Object Notation. It is a text-based format for storing and exchanging data. JSON is commonly used to transfer information between websites, applications, servers, and third-party services connected through an API.
JSON represents data in a structured form that can be read by both people and computer programs. Although the format was derived from JavaScript object syntax, it is language-independent and can be handled by most modern programming technologies.
What does JSON look like?
JSON data is often stored as an object containing key-value pairs. A product record, for example, may look like this:
{
"id": 125,
"name": "Wooden desk",
"price": 1299.99,
"available": true,
"categories": ["furniture", "office"],
"dimensions": {
"width": 140,
"height": 75
}
}In this example, keys such as "name" and "price" describe the meaning of the data, while their assigned values contain the actual product information.
What data types does JSON support?
JSON supports several basic value types:
- string – a sequence of characters enclosed in double quotation marks, such as
"New York"; - number – an integer or decimal number, such as
25or19.99; - boolean – the logical value
trueorfalse; - null – a value representing the intentional absence of data;
- object – a collection of key-value pairs enclosed in curly braces;
- array – an ordered list of values enclosed in square brackets.
Objects and arrays can be nested, allowing JSON to describe more complex structures such as orders containing multiple products, user records with several addresses, or offers with different variants.
JSON objects and arrays
A JSON object stores information as key-value pairs. Each key acts as a name identifying its assigned value:
{
"firstName": "Anna",
"age": 32
}A JSON array contains an ordered list of elements. It may hold primitive values, objects, or additional arrays:
[
{
"id": 1,
"name": "Product A"
},
{
"id": 2,
"name": "Product B"
}
]An object is suitable for describing a single item and its properties, while an array can store multiple items in a defined order.
What is JSON used for?
JSON is widely used in web applications, mobile applications, and information systems. Common uses include:
- transferring data between the front end and back end;
- receiving responses from APIs;
- submitting data from forms and applications;
- storing application settings;
- exchanging information between different systems;
- storing data in certain types of databases;
- creating configuration files;
- sending information to analytics and marketing tools.
For example, an online store may retrieve a JSON-formatted product list from an external system. The response may contain product names, prices, availability, images, and identifiers that are then displayed to the user.
JSON in API communication
One of the most common uses of JSON is exchanging data through an API. An application sends a request to a particular endpoint, and the server returns a response containing data in JSON format.
A sample API response may look like this:
{
"status": "success",
"orderId": 7842,
"total": 349.90,
"currency": "USD"
}Using JSON does not define how the API itself operates. The format describes the structure of the exchanged data, while resource addresses, request methods, authentication rules, and possible responses are specified in the documentation for the particular API.
Essential JSON syntax rules
A document must follow specific rules to be valid JSON:
- object keys must be enclosed in double quotation marks;
- string values must also use double quotation marks;
- key-value pairs are separated by commas;
- a colon separates each key from its value;
- objects are enclosed in curly braces
{ }; - arrays are enclosed in square brackets
[ ]; - a trailing comma is not allowed after the final object property or array element;
- standard JSON does not support comments;
- the values
true,false, andnullare written in lowercase.
Even a small syntax error, such as a missing quotation mark, an additional comma, or an unclosed bracket, can prevent a program from reading the data correctly.
JSON and JavaScript
JSON takes its name from JavaScript, and its syntax resembles the way objects are written in that language. However, JSON is an independent data format rather than executable program code or a complete JavaScript object.
JavaScript can contain functions, comments, undefined values, and more complex data types that standard JSON does not support. JSON data must first be converted into a structure recognized by the relevant programming language. In JavaScript, this can be done with methods such as JSON.parse() and JSON.stringify().
JSON vs. XML
Both JSON and XML can be used to store and exchange structured data. However, they use different syntax and offer different capabilities.
- JSON uses objects, arrays, and key-value pairs. Its notation is usually more concise and convenient for web applications;
- XML describes data through elements and attributes. It may be more suitable for documents requiring extensive metadata, namespaces, or strict schemas.
The appropriate format depends on the system requirements, existing integrations, and type of data. JSON is widely used by modern web APIs, while XML remains common in business systems, documents, and legacy integrations.
Is JSON a database?
JSON is not a database. It is simply a way of representing information as text. JSON data can be stored in a file, database, or application memory, or transferred between different systems.
Some databases can store complete JSON documents and query values within their structure. This does not mean that JSON itself provides storage, searching, access control, or backup functionality.
JSON validation and security
Valid JSON syntax does not guarantee that the supplied data is correct or safe. An application should verify expected fields, value types, number ranges, text length, and user permissions.
Data received from a form, API, or another external source should be treated as untrusted. It must be validated and processed appropriately before it is stored in a database, displayed on a website, or used to perform an operation.
JSON Schema can be used to describe the expected structure of JSON documents. It can define required properties, permitted data types, and additional constraints, allowing a system to check whether a document follows the established rules.