Python json.dumps and json.loads explained

Python's built-in json module handles both directions between JSON text and Python data. The names are easy to mix up, so remember that load reads JSON and dump writes JSON.

Read JSON with json.loads

Use loads when you have JSON text in a string, such as an API response. It returns normal Python data: objects become dictionaries and arrays become lists. Use json.load without the final s when you are reading from an open file object.

Write JSON with json.dumps

Use dumps to serialize a dictionary or list into a JSON string. The option indent=2 makes the result easier for people to read and review.

Handle unsupported values deliberately

Strings, numbers, booleans, lists, dictionaries and None serialize naturally. Dates, decimals, sets and custom classes need a deliberate conversion step. Avoid str() as a shortcut: it produces Python-looking text, not guaranteed JSON.

For quick visual conversions, try Python dict to JSON and see the complete conversion guide.