What is JSON?
Author: Domeny.ForSale editorial team Published
In brief: JSON is a text-based format for storing and exchanging data, used in applications, configuration files and communication with servers, among other things. It organises information using names, values and lists, so both people and programs can read it. It is not a programming language — it describes data but does not contain instructions for executing it.
What is JSON used for?
The name JSON stands for JavaScript Object Notation. Although the format originated from the notation used for objects in JavaScript, it is independent of that language. It is supported by Python, Java, PHP and C#, among others.
JSON often appears in APIs, interfaces that enable communication between programs. When a shop application retrieves a product’s name, price and availability from a server, it may receive the response in this format. The application reads the values and uses them to display the product card.
The same format is used to store program settings, export data or pass events between services. Files usually have the .json extension, but JSON data can also be transmitted without creating a separate file.
What does JSON look like, and how do you read it?
Many JSON documents are based on an object, a collection of “name–value” pairs. The name is also called a key. The following example describes a product in a shop:
{
"name": "Ceramic mug",
"price": 39.90,
"currency": "PLN",
"available": true,
"colours": ["white", "green"],
"manufacturer": {
"name": "Example Studio"
},
"promo_code": null
}
Curly braces {} indicate an object. A colon separates the key from its value, and a comma separates successive pairs. In this example, the key name has the text value Ceramic mug, while price has a numeric value.
Square brackets [] indicate an array, an ordered list of values. The colours list contains two text strings. Meanwhile, manufacturer is an object placed inside another object — this nesting allows related information to be grouped together.
JSON allows six types of values:
- text, written in double quotation marks, e.g.
"PLN"; - a number, e.g.
39.90or12; - a Boolean value:
trueorfalse; null, indicating an explicit absence of a value;- an object, written inside
{}; - an array, written inside
[].
An entire JSON document does not have to be an object. It can also be an array, a text string, a number or another permitted value, although a particular API may require a specific structure.
Syntax rules and common mistakes
JSON has strict syntax. Object keys and text values must be written in double quotation marks — single quotation marks are not a substitute. Decimal numbers use a point rather than a comma, regardless of Polish conventions for writing numbers.
You must not add a comma after the last element of an object or array. Standard JSON does not support comments either. The following notation is therefore incorrect:
{'name': 'Mug', 'price': 39.90,}
The correct version looks like this:
{"name": "Mug", "price": 39.90}
Spaces, indentation and line breaks outside text strings make the document easier to read — they do not change the meaning of the data. However, a quotation mark inside a text string must be preceded by a backslash, e.g. "Mug \"Morning\"". A backslash itself is written as \\.
Correct syntax does not necessarily mean correct data. The value "39.90" is text, while 39.90 is a number. Both are allowed in JSON, but a program expecting a number may reject text. Similarly, null, an empty text string "" and a missing key are different situations whose meaning depends on the application.
Also avoid repeating the same key name within a single object. Programs may interpret such data differently, for example by keeping only the last value.
How do you open and check a JSON file?
You can open a JSON file in an ordinary text editor. A code editor that highlights syntax, formats indentation and flags errors will be more convenient. You do not need to run a server or install a development environment just to read it.
If the file does not work in an application, check it in this order:
- Make a copy of the file before editing it.
- Check the syntax with a JSON validation tool: quotation marks, commas and closing brackets or braces.
- Compare the field names and value types with the application’s documentation.
- Check the required fields and permitted values, such as the currency code or date format.
JSON does not have a separate date type, so dates are often stored as text according to an agreed convention. Structural requirements can be described using JSON Schema — rules that specify required fields and their types, among other things.
Do not paste files containing passwords, API keys or personal data into arbitrary online validators. Check such data locally using a trusted tool.
When should you choose JSON, and when should you use another format?
JSON works well when data has a nested structure: an order contains a customer, an address and a list of products. For a simple table intended for a spreadsheet, CSV, a format that stores data in rows and columns, may be more convenient.
When integrating with someone else’s service, the format is not a matter of choice — the service’s documentation determines it. Start with an example message required by the service, change a few values and check both the syntax and whether the fields meet the requirements. This will help you distinguish a JSON syntax error from an error in the data expected by the program.
See also: What is a CDN?
