The json module in Python allows you to encode and decode JSON (JavaScript Object Notation) data. It provides built-in methods for converting Python data structures to JSON strings and vice versa, essential for working with web APIs and configuration files.
Installation:
Built-in module (No installation required)
Key Features:
- Convert Python dictionaries/lists to JSON strings (json.dumps)
- Parse JSON strings into Python data structures (json.loads)
- Read and write JSON directly to/from files (json.dump, json.load)
- Customizable serialization and formatting with indentations
Code Example:
import json
# Python dictionary to JSON string
data = {"name": "Alice", "role": "Developer", "skills": ["Python", "JSON"]}
json_str = json.dumps(data, indent=4)
print("JSON String:\n", json_str)
# Parse JSON string back to dictionary
parsed = json.loads(json_str)
print("Parsed Name:", parsed["name"])
No comments:
Post a Comment