How to get string objects instead of Unicode from JSON
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
This question only makes sense if you are precise about the Python version. In Python 3, str is Unicode text already, so asking for "string instead of Unicode" usually means you actually want bytes; in Python 2, str and unicode were separate types, which is why older answers often sound different from modern code.
What json.loads Returns
In Python 3, the json module decodes JSON strings into Python str objects.
Output:
That str value is Unicode text. There is no second "plain string" text type in Python 3.
If You Actually Need Bytes
Sometimes the real requirement is not "non-Unicode text" but raw encoded bytes for a legacy API, file format, or socket protocol. In that case, decode the JSON normally and then encode the fields you need.
Output:
This is the correct boundary: JSON gives you text, and you explicitly choose an encoding when you need bytes.
Why Python 2 Answers Look Different
In Python 2:
- '
strmeant a byte string' - '
unicodemeant text'
That is why older answers often used recursive conversion helpers after json.loads. A common pattern looked like this:
That pattern is historical. It is not the right default for Python 3 because text handling changed fundamentally.
Do Not Fight Unicode in Python 3
JSON is a text format. Python 3's json.loads returning Unicode text is the correct behavior, not a problem to work around.
For example:
Working with text as text avoids many encoding bugs. If you convert everything to bytes too early, you now have to remember the encoding at every later step.
The better mental model is:
- use
strfor text inside Python - use
bytesonly at I/O boundaries
Handling Nested Data
If you truly must convert every string value in a decoded Python 3 JSON object to bytes, do it explicitly and recursively so the behavior is obvious.
This works, but use it only when another system truly demands bytes. It makes ordinary Python data handling less convenient because dictionary keys and values stop being normal text strings.
Serializing Back to JSON
When writing JSON, keep the data as normal Python str values and let json.dumps handle encoding rules.
If you need bytes to send over the network, encode the final JSON string:
This is cleaner than forcing all internal values to bytes before serialization.
Common Pitfalls
- Assuming Python 3 has a separate non-Unicode text string type. It does not;
stralready represents Unicode text. - Converting all decoded JSON strings to bytes too early, then fighting encoding issues throughout the rest of the program.
- Reading old Python 2 advice and applying it unchanged to Python 3 code.
- Forgetting that JSON is a text format and that bytes are usually only needed at file, network, or legacy API boundaries.
- Mixing
strandbytesin comparisons or concatenation, which causes errors and confusing bugs in Python 3.
Summary
- In Python 3,
json.loadsalready returns normalstrobjects, and those are Unicode text. - If you want raw bytes, encode the specific values explicitly with an encoding such as UTF-8.
- Python 2 treated
strandunicodedifferently, which is why older answers often recommend recursive conversion. - Keep text as
strinside Python 3 programs and convert to bytes only at I/O boundaries. - The right fix is usually clearer type handling, not trying to remove Unicode from JSON parsing.
Related reading
- How to get subprocess' stdout data asynchronously?
- How to get Tensorflow tensor dimensions shape as int values?
- How to get Tensorflow tensor dimensions shape as int values?
- How to get the Cartesian product of multiple lists
- How to get the domain name of my site within a Django template?
- How to get the filename of a sample from a DataLoader?
- How to get the last N rows of a pandas DataFrame?
- How to get the line count of a large file cheaply in Python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.