Python JSON: A Comprehensive Guide
Python JSON
JavaScript Object Notation, or JSON, is a lightweight data format that is frequently used for client-server data interchange. Its simple, readable syntax makes it an efficient and straightforward choice for transmitting data. Although JSON originated from JavaScript, it is widely compatible with many programming languages, including Python, Perl, and Java.
What Is JSON?
JSON primarily supports the following six data types:
- String
- Number
- Boolean
- Null
- Object
- Array
JSON relies on two fundamental structures:
- Name/value pairs: These resemble records, objects, dictionaries, or hash tables in other languages.
- Ordered lists of values: These are equivalent to arrays, lists, or sequences.
In Python, JSON data is similar to dictionaries and lists. Below is an example of JSON data:
{
"book": [
{
"id": 1,
"language": "English",
"edition": "Second",
"author": "Derrick Mwiti"
},
{
"id": 2,
"language": "French",
"edition": "Third",
"author": "Vladimir"
}
]
}
Download New Real Time Projects :-Click here
Working with JSON in Python
To work with JSON data, Python provides the json
module. This module provides functionality for serializing (encoding) and deserializing (decoding) JSON data.
Encoding (Serialization)
Converting Python objects into JSON representation is known as serialization. This is useful when data needs to be transmitted or stored. Python’s json
module provides two main methods for serialization:
dump()
: Serializes data into a file.dumps()
: Serializes data into a JSON-formatted string.
PHP PROJECT:-Â CLICK HERE
Example: Using dump()
The dump()
method writes JSON data to a file. It takes two arguments: the data to serialize and the file object where the data will be stored.
import json
# Sample data
student = {
"Name": "Peter",
"Roll_no": "0090014",
"Grade": "A",
"Age": 20,
"Subjects": ["Computer Graphics", "Discrete Mathematics", "Data Structures"]
}
# Serialize and save to a file
with open("data.json", "w") as write_file:
json.dump(student, write_file)
Output in data.json
:
{
"Name": "Peter",
"Roll_no": "0090014",
"Grade": "A",
"Age": 20,
"Subjects": ["Computer Graphics", "Discrete Mathematics", "Data Structures"]
}
INTERVIEW QUESTION:-CLICK HERE
Example: Using dumps()
The dumps()
method converts Python objects into JSON strings without writing them to a file.
import json
# Sample data
student = {
"Name": "Peter",
"Roll_no": "0090014",
"Grade": "A",
"Age": 20
}
# Serialize to a JSON string
json_string = json.dumps(student)
print(json_string)
Output:
{"Name": "Peter", "Roll_no": "0090014", "Grade": "A", "Age": 20}
Complete Advance AI topics:- CLICK HERE
Decoding (Deserialization)
Deserialization is the process of converting JSON data back into Python objects. Python’s json
module provides two main methods for this:
load()
: Reads JSON data from a file and converts it to a Python object.loads()
: Converts JSON strings into Python objects.
Example: Using load()
The load()
method reads JSON data from a file and deserializes it into Python objects.
import json
# Load data from a file
with open("data.json", "r") as read_file:
student_data = json.load(read_file)
print(student_data)
Output:
{'Name': 'Peter', 'Roll_no': '0090014', 'Grade': 'A', 'Age': 20, 'Subjects': ['Computer Graphics', 'Discrete Mathematics', 'Data Structures']}
Complete Python Course with Advance topics:- CLICK HERE
Example: Using loads()
The loads()
method converts a JSON string into Python objects.
import json
# JSON string
json_string = '{"Name": "Peter", "Roll_no": "0090014", "Grade": "A", "Age": 20}'
# Deserialize JSON string
student_data = json.loads(json_string)
print(student_data)
Output:
{'Name': 'Peter', 'Roll_no': '0090014', 'Grade': 'A', 'Age': 20}
Pretty Printing JSON
For better readability and debugging, you can format JSON data using the indent
and sort_keys
parameters of the dumps()
method.
import json
person = {
"Name": "Andrew",
"City": "New York",
"Age": 23,
"Subjects": ["Math", "Physics", "Chemistry"]
}
# Pretty print JSON
formatted_json = json.dumps(person, indent=4, sort_keys=True)
print(formatted_json)
Output:
{
"Age": 23,
"City": "New York",
"Name": "Andrew",
"Subjects": [
"Math",
"Physics",
"Chemistry"
]
}
Summary of Key Functions
Function | Description |
---|---|
dump() | Serialize Python object to JSON file. |
dumps() | Serialize Python object to JSON string. |
load() | Deserialize JSON file into Python object. |
loads() | Deserialize JSON string into Python object. |
- python json from file
- python json load
- python json dumps
- python read json
- python json library
- python json to string
- python json to dict
- python json pretty print
Post Comment