I got the following json: {u'a': u'aValue', u'b': u'bValue', u'c': u'cValue'}
by doing request.json
in my python code. Now, I want to convert the unicode json to normal json, something which should like this: {"a": "aValue", "b": "bValue", "c": "cValue"}
. How do I get this done, without having to do any manual replacements? Please help.

- 704
- 2
- 12
- 33
-
Possible Duplicate of [Python: json.loads returns items prefixing with 'u'](http://stackoverflow.com/q/13940272) – Bhargav Rao Apr 30 '16 at 11:49
-
Whty is this bothering you? – timgeb Apr 30 '16 at 11:49
-
Has been asked many times, Did you research? [How to get string objects instead of Unicode ones from JSON in Python?](http://stackoverflow.com/q/956867) – Bhargav Rao Apr 30 '16 at 11:50
4 Answers
{u'a': u'aValue', u'b': u'bValue', u'c': u'cValue'} is a dictionary which you are calling as unicode json. Now, in your language if you want a regular json from this then just do something like this:
x={u'a': u'aValue', u'b': u'bValue', u'c': u'cValue'}
y=json.dumps(x)
print y
The output will be {"a": "aValue", "c": "cValue", "b": "bValue"}

- 435
- 1
- 5
- 17
-
14Json.dumps does not return a dictionary. It only converts it into a string. – wolfsbane Aug 22 '17 at 08:41
-
For python 2.x
import yaml
import json
json_data = yaml.load(json.dumps(request.json()))
Now this json_data can be used as a json and can have list of json as well.

- 807
- 12
- 17
-
yamal.load() depricated. https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation – Omkesh Sajjanwar Oct 12 '21 at 18:31
You can use a list comprehension to encode all of the keys and values as ascii like this:
dict([(k.encode('ascii','ignore'), v.encode('ascii','ignore')) for k, v in dct.items()])
Note: There generally isn't much benefit to not having your data in unicode, so unless you have a specific reason not to have it in unicode, then I would leave it.

- 2,175
- 1
- 17
- 20
A library available in PyPi may be helpful, see: unidecode.
It is intended to transform European characters with diacriticals (accents) to their base ASCII characters, but it does just as well when the unicode character is already in the ASCII range.
from unicode import unidecode
def fUnUn(sOrU):
return unidecode(sOrU).encode('ascii') if type(sOrU) is unicode else sOrU
sASCII = fUnUn(u'ASCII')

- 4,415
- 5
- 29
- 41