Concept and Features of JSON
Concept of JSON
JSON (JavaScript Object Notation) is a lightweight, human-readable data interchange format used to store and transfer data between systems. It is widely used in web applications, especially in APIs.
JSON is language-independent, meaning data can be created in one programming language and read in another.
Basic Characteristics of JSON
- Data is stored in name/value pairs
- Objects are enclosed in curly braces { }
- Arrays are enclosed in square brackets [ ]
- Values are separated by commas
{
"name": "BCA",
"age": 30,
"car": null
}
Features of JSON
JSON.parse() and JSON.stringify().Similarity and Difference Between XML and JSON
XML (eXtensible Markup Language) and JSON (JavaScript Object Notation) are both widely used data interchange formats for storing and transferring structured data between systems.
Similarities Between XML and JSON
Differences Between XML and JSON
| Basis | XML | JSON |
|---|---|---|
| Full Form | Extensible Markup Language | JavaScript Object Notation |
| Complexity | More complex to learn | Easy to learn |
| Data Format | Uses tags | Uses key–value pairs |
| Read/Write | More verbose | Simple and compact |
| Security | More secure | Less secure |
| Display Capability | Can display data (markup) | No display capability |
| Array Support | Does not support arrays directly | Supports arrays |
| Based on | SGML | JavaScript |
| Closing Tags | Required | Not required |
Example Comparison
<student>
<name>ABC</name>
<age>21</age>
<course>BCA</course>
</student>
{
"name": "ABC",
"age": 21,
"course": "BCA"
}
JSON Objects and Arrays
A JSON object is a collection of key–value pairs enclosed in curly braces { }. Keys are always strings in double quotes; values can be string, number, boolean, array, object, or null.
{
"key": value
}
Example — String values
{
"name": "ABC",
"course": "BCA",
"city": "Bharuch"
}
"name", "course", "city" are keys; "ABC", "BCA", "Bharuch" are string values.
{
"rollNo": 25,
"age": 20,
"fees": 15000.50
}
Numbers in JSON are written without quotes.
A JSON array is an ordered list of values enclosed in square brackets [ ]. It can contain strings, numbers, booleans, objects, or other arrays.
[
value1,
value2,
value3
]
Example — Array of strings
{
"subjects": [
"Java",
"DBMS",
"Web Technology"
]
}
Example — Array of objects
{
"employees": [
{
"name": "Sonoo",
"email": "sonoo@gmail.com"
},
{
"name": "Rahul",
"email": "rahul@gmail.com"
}
]
}
This shows how complex data can be represented using an array of objects.
Comments in JSON
Definition
JSON does not officially support comments. The JSON standard is designed to be lightweight and strict, so comment syntax like // or /* */ is not allowed in pure JSON.
Explanation
- JSON is meant for data interchange only.
- To keep parsing simple and fast, comments are excluded.
- If comments are added, many JSON parsers will throw an error.
{
// This is not allowed
"name": "ABC"
}
Adding // comments will cause a parser error.
{
"_comment": "Student information",
"name": "ABC"
}
This is valid because _comment is treated as a normal data field. Developers use dummy fields like this to add notes.