How to Use json.dumps Without Newlines for Compact Output?

16 Replies, 2281 Views

Yo, I had the same issue last week! The trick is to set `indent=None` AND `separators=(',', ':')` together.

Here’s the code snippet:
```python
import json
compact_json = json.dumps({"foo": "bar"}, indent=None, separators=(',', ':'))
print(compact_json)
```
This will give you json dumps without newlines, all in one line. If you’re still seeing newlines, maybe your IDE or terminal is adding them?

Messages In This Thread



Users browsing this thread: 1 Guest(s)