Free Online Code Editor
Parse and create JSON data with the json module.
import json
# Python dict to JSON string
data = {
"name": "Alice",
"age": 30,
"skills": ["Python", "SQL", "Git"]
}
json_str = json.dumps(data, indent=2)
print("JSON string:")
print(json_str)
# JSON string to Python dict
json_input = '{"product": "Laptop", "price": 999}'
parsed = json.loads(json_input)
print(f"\nParsed: {parsed}")
print(f"Product: {parsed['product']}")
# Write JSON to file
with open("data.json", "w") as f:
json.dump(data, f, indent=2)
print("\nJSON written to data.json")
# Read JSON from file
with open("data.json", "r") as f:
loaded = json.load(f)
print(f"\nLoaded from file: {loaded}")Use json.dumps() to convert Python to JSON string, json.loads() to parse JSON string. Use dump()/load() for file operations.