What's the best way to use json dumps in Python for clean output? or How do I handle special characte

12 Replies, 731 Views

Title: Why does json dumps in Python sometimes return escaped slashes? How to fix it?

Hey everyone,

I’ve been using json dumps in Python a lot lately, but sometimes it adds these weird escaped slashes (like `\\`) in the output.

For example, `"path": "C:\\folder"` becomes `"path": "C:\\\\folder"`. Super annoying, right?

Is there a way to avoid this? I tried playing with the `ensure_ascii` and `escape_forward_slashes` params, but no luck.

Also, does this happen only on Windows or is it a general thing?

Thanks in advance!

---

*P.S. If you’ve got tips for cleaner output with json dumps in Python, I’m all ears!*
Ah, the dreaded double slashes in json dumps in Python! It’s not just a Windows thing—happens on all platforms because JSON requires escaping backslashes.

Try setting `ensure_ascii=False` and `escape_forward_slashes=False` (if you’re using Python 3.7+).

For cleaner output, you might wanna check out `orjson` (https://github.com/ijl/orjson). It’s faster and handles slashes better.

Also, if you’re stuck with built-in json, maybe pre-process your strings to replace `\` with `/` before dumping?
Yo, this drove me nuts too! The issue is JSON specs—backslashes are escape chars, so json dumps in Python HAS to double them.

Workaround? Use raw strings (`r"C:\folder"`) or switch to forward slashes (`C:/folder`).

Btw, `ensure_ascii=False` won’t fix this—it’s about Unicode, not slashes.

If you’re lazy (like me), just live with it. Most parsers handle the escaped slashes fine anyway.
This is a classic JSON thing, not just Python. The spec says backslashes must be escaped, so json dumps in Python follows the rules.

But if you *really* hate it, try `json.dumps(your_data).replace('\\\\', '\\')`. Dirty hack, but works.

Or, as others said, use `orjson`—it’s way more flexible with slashes and way faster.

Side note: Windows paths are the worst for JSON. Maybe normalize paths first?
Ugh, I feel your pain. json dumps in Python is just doing its job—JSON needs those slashes escaped.

But here’s a pro tip: if you’re dumping paths, convert them to forward slashes first (`path.replace('\\', '/')`). Most systems handle it fine, and JSON stays clean.

Also, `orjson` is a lifesaver. No more slash drama.

Fun fact: This is why I switched to Linux for dev work. Fewer path headaches.
Wait, why is this a problem? The escaped slashes are valid JSON. Any decent parser will unescape them back to single slashes.

But if you *must* have pretty output, try `json.dumps(your_data, indent=4).replace('\\\\', '\\')`.

Or just use `orjson`—it’s like json dumps in Python but without the quirks.

Btw, this isn’t a Windows-only issue. JSON is JSON, no matter the OS.
Hey, OP here!

Thanks for all the replies—super helpful! I tried `orjson` and it’s a game-changer. No more weird slashes, and it’s way faster.

Still weird that the built-in json dumps in Python does this, but I get why now.

Follow-up Q: Anyone know why `orjson` isn’t in the standard library yet? Seems like a no-brainer.

Also, the path normalization tip saved me a ton of hassle. Cheers!



Users browsing this thread: 1 Guest(s)