s2nner
s2nner

Reputation: 23

Decode unicode in json

I have code.

# -*- coding: utf8 -*-
import re, json
from pprint import pprint

jsonStr = '{"type":"\u041f\u0435\u043d\u0438","values":{"v1":"222"}}'
data = json.loads(jsonStr)
pprint(data)

output

{u'type': u'\u041f\u0435\u043d\u0438', u'values': {u'v1': u'222'}}

how to get the normal data in 'type'?

thanks to all, beautiful output in the console

jsonStr = '{"type":"\u041f\u0435\u043d\u0438","values":{"v1":"222"}}'
data = json.loads(jsonStr.decode("utf-8"))
print json.dumps(data, sort_keys=True, indent=2).decode("unicode_escape")

output

{
"type": "Пени", 
"values": {
    "v1": "222"
  }
}

Upvotes: 0

Views: 147

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124558

You have normal data:

>>> import json
>>> jsonStr = '{"type":"\u041f\u0435\u043d\u0438","values":{"v1":"222"}}'
>>> data = json.loads(jsonStr)
>>> print data['type']
Пени

Python containers such as dictionaries and lists show their contents using the repr() function; you are looking at debugger friendly output, which is ASCII safe. To make it ASCII safe any non-ASCII and non-printable codepoints are shown as escape sequences, so you can copy that output to a Python interpreter and re-create the value safely without having to worry about codecs.

Just use the data as you normally would. I printed the string, so that Python could encode it to my terminal codec and my terminal decoded it and showed the Russian text (cyrillic characters).

Upvotes: 2

Related Questions