JSON syntax and data structure rules¶
JSON (JavaScript Object Notation) is a lightweight, text-based format used for storing and transporting data structures. It is widely used in software engineering for general data representation, configuration files, and as the standard format for exchanging data via HTTP Web APIs^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Basic Syntax Rules¶
JSON defines a simple set of rules for structuring data objects^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]:
- Curly Braces
{}: These define an object. An object is a collection of key-value pairs. - Square Brackets
[]: These define an array (or list). Arrays are ordered lists of values. - Key-Value Syntax: Data within objects is organized into pairs where a key is mapped to a value.
- Formatting:
- Keys and string values must be enclosed in double quotes.
- Keys are separated from their values by a colon (
:). - Multiple key-value pairs within an object are separated by commas.
- Items within an array are separated by commas.
Example Structures¶
Single Object¶
A simple JSON object representing a customer might look like this^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]:
{
"customerID": "a",
"firstName": "Bob",
"lastName": "Smith"
}
Array of Objects¶
To represent multiple customers (a list of objects), you use square brackets to enclose the objects^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
[
{"customerID": "1", "firstName": "James", "lastName": "Baker"},
{"customerID": "2", "firstName": "Jonathan", "lastName": "D"},
{"customerID": "3", "firstName": "Aleem", "lastName": "Janmohamed"}
]
Data Types and Serialization¶
When working with JSON in programming environments, it is important to distinguish between native class instances and JSON-compatible structures.
- JSON Serializable: JSON libraries typically handle primitive data types (strings, numbers, booleans, null, lists, and dictionaries) natively^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
- Non-Serializable Objects: Complex objects, such as custom class instances, are often not directly serializable (e.g.,
TypeError: Object of type Customer is not JSON serializable)^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
To store custom objects, they usually need to be converted into dictionaries (hash maps) where every attribute is represented as a key-value pair. In languages like Python, this can often be achieved using an object's internal dictionary representation (e.g., __dict__), which converts the object into a standard format that JSON libraries can process^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Related Concepts¶
- [[Serialization]]
- [[API]]
- [[Dictionaries]]
Sources¶
400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md