0

I have a code like this:

 import ast 
 objects = MenuModel.objects()
 data = ast.literal_eval(objects.to_json())

this code return :

    {
        "_id": {
            "$oid": "54eab211b0b7080827f3b9d3"
        }, 
        "name": "\\u0627\\u06cc\\u0646\\u0645 \\u06cc\\u0647 \\u06cc\\u0648\\u0646\\u06cc \\u06a9\\u062f", 
    }

and i can't return unicode data. How can i convert "name" filed to unicode?

1
  • why do you call ast.literal_eval() (that expects a Python literal as an input) on JSON data (as the name .to_json() implies)? Commented Feb 24, 2015 at 9:50

1 Answer 1

2
>>> import json
>>> json_text = json.dumps(u'\N{ARABIC LETTER ALEF}')
>>> json_text
'"\\u0627"'
>>> #XXX WRONG DO NOT DO IT!!!
>>> import ast; ast.literal_eval(json_text)
'\\u0627'

Do this instead if you need to convert a json text into Python object:

>>> json.loads(json_text)
u'\u0627'

Or avoid calling .to_json() and work with objects object directly (convert it to a dict if necessary).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.