Search code examples
pythonjsonsymbols

using symbols in json in python



Recently, I got a problem while working with json in python. Actually that is about special symbols in json. The problem is defined with code below:

import json
app = {
       "text": "°"
       }
print(json.dumps(app, indent=2))

but giving this I get this:

{
  "text": "\u00b0"
}

Here the ° sign is replaced with \u00b0. But I want it to be exact as my input. How can I do it?

Thanks in advance.


Solution

  • According to https://pynative.com/python-json-encode-unicode-and-non-ascii-characters-as-is/, you want to set ensure_ascii=False:

    >>> import json
    >>> app={"text": "°"}
    >>> print(json.dumps(app, indent=2, ensure_ascii=False))
    {
      "text": "°"
    }