Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence

Cover Image for Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Blog Post: Saving UTF-8 Texts with json.dumps

Do you find it frustrating when your JSON dumps include \u escape sequences instead of human-readable UTF-8 characters? Well, you're not alone! Many developers face this issue when they need to verify or edit text files with JSON dumps. But fear not, because in this blog post, we're going to address this problem head-on and provide you with easy solutions. Let's dive in! 💪

Understanding the Issue

The problem lies in how the json.dumps function handles UTF-8 texts. By default, it escapes non-ASCII characters using \uXXXX notation, which can be cumbersome and hard to read for us humans. Let's take a look at an example to understand this better:

import json

json_string = json.dumps("ברי צקלה")
print(json_string)

Output:

"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"

As you can see, the output contains escape sequences instead of the actual UTF-8 characters. This can be a real pain when you're dealing with large JSON files or when you want to manually verify the data.

The Solution: Ensuring UTF-8 JSON Strings

Fortunately, there's a straightforward solution to this problem. The json.dumps function provides an option to specify the encoding format. To save UTF-8 texts as UTF-8 in the JSON file, you can use the ensure_ascii=False parameter. Let's modify our previous example to include this fix:

import json

json_string = json.dumps("ברי צקלה", ensure_ascii=False)
print(json_string)

Output:

"ברי צקלה"

Hooray! 🎉 Now our JSON string contains the actual UTF-8 characters instead of escape sequences. It's so much easier to read and work with, especially for your users who might need to verify or edit text files with JSON dumps.

A Better Approach: Writing to a File

While printing the JSON string is useful for demonstration purposes, it's often more practical to write the JSON data to a file. Let's see how we can achieve that:

import json

data = "ברי צקלה"

with open("output.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False)

print("Data saved to output.json")

In this example, we're opening a file called "output.json" in write mode and specifying the encoding as UTF-8. Then, we're using the json.dump function to write our data to the file with the ensure_ascii=False parameter. Now you have a JSON file with human-readable UTF-8 characters!

Your Turn to Try

Now that you've learned the easy solutions to saving UTF-8 texts with json.dumps, it's time to put your new knowledge into practice. Try it out in your own projects and see the difference it makes. Don't forget to share your results with us! We'd love to hear about your experiences. 😊

Wrapping Up

In this blog post, we addressed the common issue of saving UTF-8 texts with json.dumps as \u escape sequences instead of human-readable characters. We provided easy solutions to ensure UTF-8 JSON strings, both for printing and writing to files. Now you can confidently work with JSON dumps and empower your users with more readable data.

If you found this article helpful, or if you have any questions or suggestions, please let us know in the comments below. Happy coding! 💻✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello