liveBook · Manning

Json.dump()

As explained in A Pythonic Adventure: From Python basics to a working web app

The Python json.dump function serializes a Python object as JSON and writes the result to a file-like object.

Use json.dump() to turn a Python dictionary into JSON and write it to an opened file. Pass the dictionary first and the file object second. section 8.0

Save a coffee order

Import json, open orders.json for writing, dump the order, and close the file. Add indent=4 when you want the saved JSON to be easier to read. You can also dump a list of orders. section 8.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Explains the two required arguments, optional indentation, and file closing, but does not examine serialization internals.
Examples
Uses coffee orders, orders.json, a list of orders, and readable indent=4 output.
What it assumes
Assumes basic Python dictionaries and file opening while teaching the json import and file-writing workflow.
What you can do after
Apply it via Python’s json module to save dictionaries or lists as JSON files.
How it connects
Connects dictionaries, JSON, file objects, open(), close(), and json.load().

Also explained in

Compare how 3 books explain json.dump() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.