What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange text format that is so easy for humans to read and write, and also works easily for machines to parse and generate.
Why JSON?
When we have a question, why JSON is used, it’s widely used to transmit data between a server and a web application, or between different parts of a software system.
How JSON Syntax Works
JSON (JavaScript Object Notation) follows a strict and simple syntax designed to be both human-readable and machine-parseable. Here’s a breakdown of how its syntax works:
Basic JSON Structure
JSON is built on two structures:
Objects (unordered key/value pairs)
Syntax:
{
"key": "value"
}
Arrays (ordered lists of values)
Syntax:
[
"value1",
"value2"
]
JSON Syntax Rules
Rule Description | Example |
---|---|
Data is in name/value pairs | "name": "John" |
Data is separated by commas | "age": 25, "city": "NY" |
Curly braces hold objects | { "name": "John", "age": 25 } |
Square brackets hold arrays | "colors": ["red", "green", "blue"] |
Keys must be strings | Must be in double quotes: "name" |
Values can be | string, number, object, array, boolean, or null |
Example JSON
{
"name": "Alice",
"age": 30,
"isStudent": false,
"skills": ["Python", "JavaScript"],
"address": {
"city": "New York",
"zip": "10001"
},
"middleName": null
}
Types of JSON Data
In JSON, data is represented using a limited set of data types that are simple and widely supported across programming languages. Here are the six core data types in JSON:
1. String
A sequence of characters enclosed in double quotes (“)
Must use double quotes (not single).
Example:
"name": "Alice",
2. Number
Can be an integer or a floating-point number.
No quotes around numbers.
Example:
"age": 25,
"height": 5.8
3. Boolean
Represents a logical value: true or false.
Example:
"isStudent": false
4. Array
An ordered list of values (can be of mixed types).
Enclosed in square brackets [].
Example:
"skills": ["Python", "JavaScript", "C++"]
5. Object
A collection of key-value pairs.
Enclosed in curly braces {}.
Keys must be strings; values can be any valid JSON type.
Example:
"address": {
"city": "New York",
"zip": "10001"
}
6. null
Represents an empty or non-existent value.
Example:
"middleName": null
These types can be nested inside each other, allowing complex data structures like arrays of objects or objects containing arrays.
Here, We are providing a list of examples of JSON data.
1. JSON data example with simple employee object:
{
"name": "Alice",
"age": 25,
"city": "New York"
}
2. JSON data example Employee object with nested address:
{
"id": 2,
"name": "Jane Smith",
"position": "HR Manager",
"department": "Human Resources",
"salary": 68000,
"address": {
"street": "123 Elm Street",
"city": "Los Angeles",
"state": "CA",
"zipcode": "90001"
}
}
3.JSON data example employee object with skills array:
{
"id": 3,
"name": "Emily Johnson",
"position": "Data Analyst",
"department": "Analytics",
"skills": ["Excel", "SQL", "Python", "Tableau"],
"fullTime": true
}
4.JSON data example list of multiple employees (array of objects):
[
{
"id": 4,
"name": "Michael Brown",
"position": "Project Manager",
"department": "Operations"
},
{
"id": 5,
"name": "Laura Wilson",
"position": "UX Designer",
"department": "Design"
}
]
5.JSON data example Detailed employee with joining date and reporting manager:
{
"id": 6,
"name": "Chris Evans",
"position": "Marketing Lead",
"department": "Marketing",
"joiningDate": "2022-08-15",
"reportingManager": "Sarah Connor"
}