Python
JSON
UTF-8
Encoding
Serialization

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When working with JSON data in Python, particularly with text that includes non-ASCII characters, you may encounter challenges related to encoding. By default, Python's json.dumps method encodes non-ASCII characters using escape sequences (e.g., converting "é" to "\u00e9"). This can cause issues when the JSON data is expected to be human-readable or needs to maintain specific encoding guidelines, such as UTF-8. In this article, we explore how to save UTF-8 texts using json.dumps without converting them to escape sequences.

Using json.dumps with UTF-8 Encoding

To store UTF-8 text via JSON without the escape sequences, the json.dumps function in Python provides the ensure_ascii parameter. By default, ensure_ascii is set to True, which means non-ASCII characters will be escaped. Setting ensure_ascii to False allows these characters to be output directly as UTF-8.

Example

Here is an example demonstrating how to use json.dumps to encode UTF-8 texts correctly:

python
1import json
2
3# Sample data with non-ASCII characters
4data = {
5    "message": "Café",
6    "town": "München",
7}
8
9# Convert to JSON with non-ASCII characters preserved
10json_utf8 = json.dumps(data, ensure_ascii=False)
11
12print(json_utf8)  # Output: {"message": "Café", "town": "München"}

In this example, the ensure_ascii=False parameter ensures that "Café" and "München" are preserved in their original form, and are not converted into their escaped versions.

The Benefits of Using UTF-8 Directly

Human-Readability

One of the chief advantages of using UTF-8 encoding directly is improved human readability. When inspecting JSON outputs manually or sharing data over systems where human inspection is required, maintaining text in its natural form is often more efficient and less error-prone.

System Compatibility

Some systems or APIs may require JSON data to maintain specific encoding standards like UTF-8. Encoding data appropriately can prevent errors during data transfers and processing and ensure that systems correctly interpret the data.

Encoding Non-ASCII Characters: Summary

Here is a summary table highlighting the key differences between using escape sequences and UTF-8 encoding in JSON data.

AspectEscape SequencesUTF-8 Encoding
Encoding Parameterensure_ascii=True (default behavior)ensure_ascii=False
Output FormatConverts non-ASCII to \u sequences (e.g., "München" becomes "\u00fc")Maintains original characters (e.g., "München")
ReadabilityLess human-readableMore human-readable
External SystemMay require additional decoding steps or have compatibility issuesCompatible with UTF-8 systems

Additional Considerations

Compatibility

When opting for UTF-8 encoding, ensure that the consuming applications, systems, or services are equipped to handle UTF-8 data correctly. While it is a common standard, assumptions about the consuming systems should always be verified.

JSON Files and UTF-8 Encoding

When saving JSON content to a file that includes UTF-8 encoded data, it is critical to ensure that the file itself is saved and read with UTF-8 encoding.

Example of Writing to a File in UTF-8:

python
1# File path
2file_path = 'data.json'
3
4# Writing JSON data to a file with UTF-8 encoding
5with open(file_path, 'w', encoding='utf-8') as f:
6    json.dump(data, f, ensure_ascii=False)

In this context, the file is explicitly opened with encoding='utf-8' to ensure the JSON output maintains its UTF-8 encoding when stored.

Conclusion

Encoding and interpreting texts correctly is crucial when dealing with JSON data, especially when it includes non-ASCII characters. By manipulating the json.dumps function with ensure_ascii=False, you can preserve the original UTF-8 encoding, enhancing readability and compatibility. Always ensure that both the data processing and consuming ends are designed to correctly handle UTF-8 to prevent encoding-related issues.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions