What is JSON?
What is JSON?
JSON stands for JavaScript Object Notation. It is a lightweight data storage and communication protocol that uses JavaScript syntax. JSON stores data in key-value pairs and it is primarily used in REST services to exchange data.
It is easy for humans to read and write, and easy for machines to parse and generate. JSON is primarily used to transmit data between a server and a web application or between different parts of an application.
The file extension for a JSON file is .json
For example, a file containing JSON data might be named as follows:
data.json
config.json
response.json
Features of JSON
Some of the features of JSON are as follows:
- Data format: It represents data in the form of key-value pairs, similar to how objects or dictionaries work in programming languages.
- Human-readable: The syntax is simple and easy for humans to understand and edit.
- Language-independent: Although it originated from JavaScript, it is supported by most programming languages, including Python, Java, PHP, and more.
Structure of JSON
JSON has two main components:
- Objects: Enclosed in curly braces
{}
and contain key-value pairs. Keys must be strings, and values can be strings, numbers, arrays, other objects, or booleans. - Arrays: Ordered lists of values, enclosed in square brackets
[]
.
Example of JSON
{ "name": "John", "age": 30, "isStudent": false, "address": { "street": "123 Main St", "city": "New York" }, "courses": ["Math", "Science", "History"] }
In this example:
- The outer structure is an object.
- The keys are
"name"
,"age"
,"isStudent"
,"address"
, and"courses"
. - The value of
"address"
is another object, while"courses"
is an array.
JSON is widely used in APIs and web services for data exchange because of its simplicity and efficiency.