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.
(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.
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.
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.
If you have a Python object, you can convert it into a JSON string by using the json. dumps() method.
{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"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With