json.dumps() vs json.loads()
json.dumps() and json.loads() are both Python JSON Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. json.dumps(): Serializes a Python object to a JSON formatted string. Its syntax is json.dumps(obj, *, skipkeys=False, ensure_ascii=True, indent=None, separators=None, default=None, sort_keys=False), and it returns jSON string json.loads(): Deserializes a JSON string to a Python object. Its syntax is json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None), and it returns python object Example of json.dumps(): import json
json.dumps({"a": 1, "b": 2}) # '{"a": 1, "b": 2}'
json.dumps({"a": 1}, indent=2) # Pretty-printed. Example of json.loads(): json.loads('{"a": 1}') # {"a": 1}
json.loads('[1, 2, 3]') # [1, 2, 3]. Read the full references for json.dumps() and json.loads() to see parameters, edge cases, and example code side by side. Other JSON Module APIs worth comparing: json.dump(), json.load(), json.JSONEncoder(), json.JSONDecoder().