JSON Basics
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for humans to read and write and simple for machines to parse and generate. It is language-independent and widely used for transmitting data between a server and web application.
JSON Structure
JSON supports two primary data structures:
- Objects: Enclosed in curly braces
{}
- Arrays: Enclosed in square brackets
[]
JSON Object Example
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
JSON Array Example
[
"apple",
"banana",
"cherry"
]
Data Types in JSON
JSON supports several basic data types:
Data Type |
Description |
Example |
String |
Text enclosed in quotes |
"Hello World" |
Number |
Numeric values |
42, 3.14 |
Boolean |
True or false values |
true, false |
Null |
Represents empty value |
null |
Object |
Collection of key-value pairs |
{"key": "value"} |
Array |
Ordered list of values |
[1, 2, 3] |
JSON Syntax Rules
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
Use Cases in Java Development
JSON is commonly used for:
- Configuration files
- API responses
- Data storage
- Data exchange between web services
At LabEx, we recommend mastering JSON parsing for efficient software development.
Advantages of JSON
- Lightweight and easy to read
- Language independent
- Supports nested structures
- Widely supported across programming languages
graph TD
A[JSON Data] --> B{Parsing}
B --> |Java| C[Java Objects]
B --> |Python| D[Python Objects]
B --> |JavaScript| E[JavaScript Objects]