Introduction to JSON Concepts
1. Introduction to JSON
JSON (JavaScript Object Notation) is a lightweight, text-based data-interchange format. It is language-
independent but uses conventions that are familiar to programmers of the C-family of languages,
including C, C++, C#, Java, JavaScript, Perl, and Python. Its primary purpose is to store and transport data,
often acting as the data format for asynchronous browser-server communication (AJAX).
2. Keys and Values
The basic structure of JSON is built on key-value pairs. This structure is realized as an object, a collection
of name/value pairs.
• A Key (or name) is always a string enclosed in double quotes.
• A Value can be a string, number, object, array, boolean, or null.
• The key and value are separated by a colon :.
Syntax:"key":value
Example:"username":"student_01"
3. Types of Values
JSON supports a specific set of data types to ensure cross-platform compatibility:
• String: A sequence of zero or more Unicode characters, wrapped in double quotes. Backslash
escapes are used for special characters.
• Number: Follows JavaScript’s double-precision floating-point format. It can be an integer or a
fraction.
• Boolean: Represented by the literals true or false.
• Null: Represented by the literal null to indicate an empty value.
• Object: A structural type containing a comma-separated list of key-value pairs.
• Array: A structural type containing an ordered list of values.
4. JSON Objects
An object is an unordered set of name/value pairs. It begins with a left brace { and ends with a right
brace }. Each name is followed by a colon, and the pairs are separated by commas.
Object = {
"id": 456,
"status":"active",
"score": 98.6
}
5. JSON Arrays
An array is an ordered collection of values. It begins with a left bracket [ and ends with a right bracket ].
Values within an array are separated by commas and do not require keys. Arrays can contain mixed data
types, including nested objects or other arrays.
Array = [
"Data Structures",
2024,
{"credits": 3},
true
]
Hierarchy and Nesting
JSON’s power lies in its ability to nest these structures. An object can contain an array as a value, and that
array can, in turn, contains several nested objects.
Root = {
"classroom":"301-A",
"students": [
{"name":"Alice","grade":"A"},
{"name":"Bob","grade":"B"}
]
}