1.1 The Rise of JSON
JSON (JavaScript Object Notation) is a text format used for data interchange. It was developed in the early 2000s by Douglas Crockford and became popular thanks to its simplicity and ease of use.
The main idea of JSON is to create a format that's easy for humans to read and also simple for programs to parse and generate. Although JSON was originally based on JavaScript syntax, its format was so universal that it began to be used and supported by many other programming languages. Now JSON can be used independently of JavaScript: it has become a standard for data exchange in various systems and applications, regardless of what programming language they are written in.
JSON is essentially a record of an object's state in JavaScript language. Well, almost. That's why JSON is especially popular among those who write APIs for the frontend. Additionally, JSON is widely used for storing configuration files and logs.
Main Features of JSON
Human Readability:
The JSON format is easy for people to read and understand. This makes it convenient for use in configuration files and for data exchange between client and server.
Language Independence:
Although JSON is initially based on JavaScript syntax, it can be used with any programming language. Many programming languages provide built-in libraries for working with JSON.
Ease of Parsing:
Parsing is the process of analyzing a data structure, typically text or code, to extract meaningful information. In the realm of programming, parsing is often applied to process text data. JSON can be easily parsed and generated both on the client and server side. Nowadays, every programming language has a library that allows you to easily convert an object to JSON and back.
Standard and Clear Leader:
Ten years ago, JSON was competing with XML as the API standard. Now, JSON has won and is the clear leader. In your backend API, you can support just JSON, and that will be enough, even if your API is meant to be called by other servers.
1.2 JSON Structure
JSON represents a set of "key-value" pairs, and its syntax consists of two main structures:
Objects: Objects are unordered sets of "key-value" pairs. Keys are strings, and values can be any data type supported by JSON.
{
"name": "John",
"age": 30,
"isStudent": false,
"courses": ["Math", "Science"],
"address": {
"city": "New York",
"zipCode": "10001"
}
}
Arrays/Lists: Arrays are ordered sets of values (similar to lists in Python). Values can be any data type supported by JSON.
["Apple", "Banana", "Cherry", 100, 3.14, true]
Looks a lot like Python objects, doesn't it?
The thing is, JSON is basically the standard for data representation in JavaScript, and JavaScript was inspired by Python. So you're gonna enjoy using JSON.
1.3 Data Types in JSON
JSON supports the following data types:
Strings: Represented in double quotes.
"name": "John"
Numbers: Can be integers or floats.
"age": 30
Objects: Unordered sets of "key-value" pairs.
"address": {
"city": "New York",
"zipCode": "10001"
}
Arrays/Lists: Ordered sets of values.
"courses": ["Math", "Science"]
Boolean values: true
or false
.
"isStudent": false
Unlike Python, true
and false
are written in lowercase here.
null
: Represents a null value.
"middleName": null
Date and Time: Transmitted as a string.
{
"datetime": "2023-05-15T14:30:00Z",
"date": "2023-05-15",
"time": "14:30:00"
}
In the past, they were sometimes sent as a number in UNIX timestamp format, but that approach is outdated.
GO TO FULL VERSION