Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert unicode json to normal json in python

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.

like image 962
Sanjiban Bairagya Avatar asked Apr 30 '16 11:04

Sanjiban Bairagya


People also ask

Is Unicode valid in JSON?

(in Introduction) JSON text is a sequence of Unicode code points. The earlier RFC4627 stated that, (in §3) JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

How do you change a Unicode to a string in Python?

To convert Python Unicode to string, use the unicodedata. normalize() function. The Unicode standard defines various normalization forms of a Unicode string, based on canonical equivalence and compatibility equivalence.

How do you change Unicode to ASCII in Python?

In summary, to convert Unicode characters into ASCII characters, use the normalize() function from the unicodedata module and the built-in encode() function for strings. You can either ignore or replace Unicode characters that do not have ASCII counterparts.

How do you convert to JSON in Python?

If you have a Python object, you can convert it into a JSON string by using the json. dumps() method.


1 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"}

like image 92
nirprat Avatar answered Oct 01 '22 17:10

nirprat