Categories
Python Answers

How to get string objects instead of Unicode from JSON with Python?

Spread the love

Sometimes, we want to get string objects instead of Unicode from JSON with Python.

In this article, we’ll look at how to get string objects instead of Unicode from JSON with Python.

How to get string objects instead of Unicode from JSON with Python?

To get string objects instead of Unicode from JSON with Python, we can use the encode method.

For instance, we write

nl = json.loads(js)
nl = [s.encode('utf-8') for s in nl]

to call json.loads to load the js JSON string array string into the nl list.

Then we loop through the nl list to call encode with 'utf-8' on each string in the nl list to convert them to byte strings and put the converted strings in a new list.

Finally, we assign the returned list to `nl.

Conclusion

To get string objects instead of Unicode from JSON with Python, we can use the encode method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *