The JSON library in Python provides functions for encoding Python objects into JSON strings and decoding JSON strings into Python objects. It allows for easy serialization and de-serialization of data, facilitating data interchange between different systems or programming languages. For example, to encode a Python dictionary into a JSON string, you would use the json.dumps()
function, and to decode a JSON string back into a Python dictionary, you would use json.loads()
. Here's a simple example:
Code Block | ||
---|---|---|
| ||
# Encoding Python dictionary to JSON string data = {"name": "Johnny", "last_name": "Silverhand", "age": 34, "city": "Night City"} json_string = json.dumps(data) print(json_string) # Decoding JSON string back to Python dictionary decoded_data = json.loads(json_string) print(decoded_data) |
...