[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, 922 Views

Here’s a quick example for json.dump python:

```python
import json

my_data = {"fruit": "apple", "count": 3}
with open("output.json", "w") as f:
json.dump(my_data, f, indent=2) # indent=2 for cleaner output
```

The `indent` param is just for readability—no functional impact. If your file’s empty, you might be writing to the wrong directory or not closing the file properly.

Try printing `my_data` first to ensure it’s valid. If it’s not a basic type, json.dump will fail silently.

Messages In This Thread



Users browsing this thread: 1 Guest(s)