-3

In python simplejson my dictionary is like

>>> s= {u'hello': u"Hi, i'm here"}
>>> simplejson.dumps(s)
'{"hello": "Hi, i\'m here"}'

But I want like

'{"hello": "Hi, i'm here"}'

How to do that?

user2605977
  • 269
  • 4
  • 12
  • 2
    This is exactly the same mistake in understanding as [your other question](http://stackoverflow.com/q/20658032/102441) – Eric Dec 18 '13 at 12:41
  • 1
    Note that there's a built in `json` module as of python 2.6 – Eric Dec 18 '13 at 12:43

2 Answers2

2

What you're seeing is only an internal representation. Python keep it that way so it can escape the quote you're having there.

If you print it, it will appear like normal.

>>> import json
>>> s = '{"hello": "Hi, i\'m here"}'
>>> print(s)
{"hello": "Hi, i'm here"}
aIKid
  • 26,968
  • 4
  • 39
  • 65
  • I am returning this with HttpResponse in Django? – user2605977 Dec 18 '13 at 12:46
  • I want without using print – user2605977 Dec 18 '13 at 12:47
  • 1
    Why would you return the `repr()` of a string to an HTTP response?? – Wooble Dec 18 '13 at 12:47
  • When you're using it, the backslash is never there. It only appears when you type it to the shell, which invokes `repr` to show the internal representation of your string. You can try to split, strip, or replace the backslash, it would never work. It just isn't there.. – aIKid Dec 18 '13 at 12:49
  • @Wooble: I think the OP does't understand the difference between the repr of a string and the underlying string – Eric Dec 18 '13 at 12:49
0

Python is telling you the repr of the string - how to create the string with python syntax. If you want want to see the actual contents of the string, print it:

>>> s= {u'hello': u"Hi, i'm here"}
>>> print simplejson.dumps(s)
{"hello": "Hi, i'm here"}
Eric
  • 95,302
  • 53
  • 242
  • 374
  • I want without using print – user2605977 Dec 18 '13 at 12:45
  • @user2605977: All that `print` is doing here is showing you that the ``\`` is an artifact of the python interpreter output. Your variable does not contain that slash character – Eric Dec 18 '13 at 12:47