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.