Mohammad Kamrul Hasan
Mohammad Kamrul Hasan

Reputation: 137

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.

Upvotes: 0

Views: 801

Answers (1)

larsks
larsks

Reputation: 311258

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": "°"
}

Upvotes: 2

Related Questions