import json is a Python import that serializes and deserializes JSON data. Use json.dumps() to convert Python objects to JSON strings, and json.loads() to parse JSON strings into Python dicts and lists.
# Python # description: parse JSON string, serialize to JSON import json # Parse JSON string data = json.loads('{"name": "Alice", "age": 30}') print(data['name']) # Alice # Serialize Python dict to JSON string person = {"name": "Bob", "age": 25, "skills": ["Python", "C++"]} json_str = json.dumps(person, indent=2) print(json_str) # Read JSON from file with open("config.json") as f: config = json.load(f) # Write JSON to file with open("output.json", "w") as f: json.dump(config, f, indent=2)
json.loads(string): parse JSON string into Python objectjson.dumps(obj): serialize Python object to JSON stringjson.load(file): parse JSON from filejson.dump(obj, file): write Python object as JSON to fileindent=N: pretty-print with N-space indentationsort_keys=True: sort object keys in output