0

How can I print decoded_json below so that the emoji appears?

>>> raw_json = '"smile "'
>>> decoded_json = cjson.decode(raw_json)
>>> decoded_json
u'smile \xf0\x9f\x98\x8a'

>>> print decoded_json
smile ð

>>> print 'smile \xf0\x9f\x98\x8a' # u' removed
smile 

It seems like cjson.decode returns a u' unicode string. That unicode string has the correct byte representation of the emoji, but when the string is printed some other character appears instead of the emoji. When I print the same string with u' removed, it works.

Is there something I can do to decoded_json so that it will print the emoji?

Allan
  • 12,117
  • 3
  • 27
  • 51
newbie
  • 41
  • 4
  • Possible duplicate of [Removing u in list](https://stackoverflow.com/questions/9773121/removing-u-in-list) – tripleee Mar 05 '19 at 08:15
  • What is cjson? Why are you still on an ancient Python version that will be end of life end of this year? – RemcoGerlich Mar 05 '19 at 08:18
  • 1
    Anyway raw bytes in a Unicode string is never correct, that's not the correct representation. Which bytes are in raw_json? – RemcoGerlich Mar 05 '19 at 08:19

2 Answers2

2

Add the proper coding on top of your .py files and use the json module.

Python used: (as yours)

$ python --version
Python 2.7.14+

Code:

# -*- coding: utf-8 -*-
import json

raw_json = '"smile "'
decoded_json = json.loads(raw_json)
print decoded_json
print 'smile \xf0\x9f\x98\x8a'

output:

python unicode.py
smile 
smile 
Allan
  • 12,117
  • 3
  • 27
  • 51
1

Use built-in json module:

import json
raw = '{"": "smile"}'
print(json.loads(raw))

enter image description here

Amir
  • 1,885
  • 3
  • 19
  • 36