JSON Tutorial
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is widely used in web development, APIs, and configuration files.
1. Basics of JSONβ
JSON represents data as key-value pairs enclosed within curly braces ({}):
{
"name": "John Doe",
"age": 30,
"location": "New York"
}
Rulesβ
- Keys must be strings, enclosed in double quotes.
- Values can be strings, numbers, objects, arrays, booleans, or
null. - Data is separated by commas.
2. Data Typesβ
Stringsβ
Strings are enclosed in double quotes:
{
"name": "John Doe"
}
Numbersβ
Numbers do not need quotes:
{
"age": 30
}
Booleans and Nullβ
Booleans are represented as true or false, and null values are represented as null:
{
"isActive": true,
"middleName": null
}
3. Arraysβ
Arrays are ordered lists enclosed in square brackets ([]).
{
"hobbies": ["reading", "hiking", "coding"]
}
Arrays can contain mixed data types, including other arrays and objects.
4. Nested Objectsβ
Objects can be nested within other objects:
{
"person": {
"name": "John Doe",
"age": 30
},
"location": "New York"
}
5. JSON in APIsβ
JSON is often used as the data format in APIs. For example, a response from an API might look like:
{
"status": "success",
"data": {
"id": 123,
"name": "Sample Item"
}
}
6. JSON Schemaβ
JSON Schema defines the structure and types expected in JSON data, used for validating JSON objects.
Example Schemaβ
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"isActive": { "type": "boolean" }
},
"required": ["name", "age"]
}
Summaryβ
This tutorial covered JSON basics:
- Representing data with key-value pairs.
- Using JSON data types, including strings, numbers, booleans, arrays, and objects.
- Working with JSON in nested structures and APIs.
- Introducing JSON schema for data validation.
JSONβs simplicity and readability make it a widely-used format for data interchange and storage.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.