[b]"How to properly use json.dump in Python to save data to a file?"[/b] or [b]"Why is my json.dump in Python not

14 Replies, 894 Views

json.dump python is easy-peasy once you avoid the traps. Example:

```python
import json

stuff = {"key": "value"}
with open("stuff.json", "w") as f:
json.dump(stuff, f)
```

Empty file? Probably a permission issue or the file’s already open elsewhere.

Indent is purely cosmetic—makes the json pretty. Use it if you’re sharing the file or debugging.

For complex data, check out `json.JSONEncoder` or convert to strings first.

Messages In This Thread



Users browsing this thread: 1 Guest(s)