JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy to read and write for humans and easy to parse and generate for machines. It is commonly used for transmitting data between a web server and a client. Although it originated from JavaScript, JSON is language-agnostic and is supported by many programming languages, including Python.
• String
• Number
• Boolean
• Null
• Object (equivalent to Python dictionaries)
• Array (equivalent to Python lists)
• Name/Value pairs: Similar to Python dictionaries (key-value pairs).
• Ordered lists of values: Similar to Python lists or arrays.
Python provides a built-in json module to work with JSON data. This module allows for the encoding (serialization) and decoding (deserialization) of JSON data.
Serialization refers to the process of converting Python objects into JSON format. Python offers two methods for this:
• dump(): Converts Python objects into JSON format and writes them to a file.
• dumps(): Converts Python objects into a JSON string.
The following JSON items are created from Python objects. Following is a list of each:
| SR. | JSON | Python |
|---|---|---|
| 1 | Object | dict |
| 2 | Array | list |
| 3 | String | str |
| 4 | number(int) | int |
| 5 | true | True |
| 6 | false | False |
| 7 | null | None |
import json
data = {
"name": "John",
"age": 30,
"city": "New York"
}
with open("data.json", "w") as write_file:
json.dump(data, write_file) # Write JSON data to a file
In this example, we write a Python dictionary (data) to a JSON file (data.json) using json.dump().
import json
data = {
"name": "John",
"age": 30,
"city": "New York"
}
json_string = json.dumps(data) # Convert to JSON string
print(json_string)
{"name": "John", "age": 30, "city": "New York"}
In this case, we convert a Python dictionary into a JSON string using json.dumps().
Deserialization is the process of converting JSON data back into Python objects. Python offers two methods for this:
• load(): Reads JSON data from a file and converts it into a Python object.
• loads(): Converts a JSON string into a Python object.
import json
with open("data.json", "r") as read_file:
data = json.load(read_file) # Read JSON data from a file
print(data)
import json
json_string = '{"name": "John", "age": 30, "city": "New York"}'
data = json.loads(json_string) # Convert JSON string to Python object
print(data)
{'name': 'John', 'age': 30, 'city': 'New York'}
In this case, we convert a JSON string into a Python dictionary using json.loads().
| Method | Input Type | Output Type |
|---|---|---|
| json.load() | File | Python Object |
| json.loads() | String | Python Object |
| json.dump() | Python Object | JSON File |
| json.dumps() | Python Object | JSON String |
To make JSON data easier to read and debug, we can use the indent and sort_keys parameters in the dumps() and dump() methods.
import json
data = {
"name": "John",
"age": 30,
"city": "New York"
}
json_string = json.dumps(data, indent=4, sort_keys=True)
print(json_string)
{
"name": "John",
"age": 30,
"city": "New York"
}
In this case, the indent parameter specifies how many spaces should be used for indentation, and sort_keys=True sorts the keys alphabetically.
• Encoding is the process of converting Python objects into JSON format (serialization).
• Decoding is the process of converting JSON data back into Python objects (deserialization).
In Python, json.dumps() and json.dump() are used for encoding, while json.loads() and json.load() are used for decoding.
1. Readable and Lightweight: JSON is human-readable and uses fewer characters compared to XML.
2. Language-Agnostic: JSON can be used with most programming languages (Python, JavaScript, Java, etc.).
3. Widely Used for APIs: JSON is the most commonly used format for API data exchange between a client and a server.
4. Supports Complex Data Structures: JSON supports nested objects and arrays, making it highly flexible.