JSON (JavaScript Object Notation)
JSON (JavaScript Object Notation)
JSON is a lightweight data format used for exchanging and storing data. It is easy for humans to read and write and easy for machines to parse and generate. JSON is widely used in APIs, web applications, and data storage.
Key Features of JSON:
-
Lightweight & Fast
- Uses a minimal structure, making it faster than XML.
-
Human-Readable & Easy to Write
- JSON syntax is simple and looks like JavaScript objects.
-
Language-Independent
- Supported by almost all programming languages, including JavaScript, Python, PHP, Java, C#, and X++.
-
Uses Key-Value Pairs
- Data is stored in
key: valueformat.
- Data is stored in
-
Commonly Used in APIs
- RESTful APIs and OData services return data in JSON format.
JSON Example
Simple JSON Object
{
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"isStudent": false
}
JSON with an Array
{
"employees": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
}
JSON message format
JavaScript Object Notation (JSON) is a lightweight data-interchange format. JSON is self-describing and easy for humans to read and write. It is the most commonly used data format on the web and for RESTful web services.
The following is a simple example of the JSON format describing customer group data containing two customer groups with customer group IDs of 10 and 20, with the additional Description and PaymentTermId fields:
{
"CustomerGroupId":"10",
"Description":"Wholesales customers",
"PaymentTermId":"Net30"
},
{
"CustomerGroupId":"20",
"Description":"Retail customers",
"PaymentTermId":"Receipt"
}
Another little complex example of the JSON data format describing personal details is shown here. The following example represents personal details, including address, phone number, and children and spouse details:
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
As we mentioned earlier, this is easily readable by humans, and at the same time, lighter and easy to parse by a computer program. These characteristics make JSON the preferred data type for web and cloud applications.
JSON vs XML: Why JSON is Preferred?
| Feature | JSON | XML |
|---|---|---|
| Readability | Easy to read | More complex |
| Size | Smaller | Larger |
| Parsing Speed | Fast | Slow |
| Supported in APIs | Mostly used in REST APIs | Used in SOAP APIs |
| Data Structure | Key-Value pairs | Tags and attributes |
Where is JSON Used?
✅ APIs (REST, OData, GraphQL)
✅ Web & Mobile Applications
✅ Database Storage (NoSQL databases like MongoDB)
✅ Configuration Files (.json for settings in applications)