Quotes causing problem with Python and simplejson

Python logoMy little Django and Facebook project needed to do some JSON processing, time for using simplejson then.

My application will receive a string of JSON and transform it to a dictionary, pretty simple. Let's run a test first, starting with the Python shell, or better yet, iPython, and importing the module:

import simplejson

Now let's define a string using JSON notation and pass it to simplejson:

band = "{'name': 'Mando Diao', 'genre': 'rock and roll', 'bid': 19383}"
json = simplejson.loads(band)

And we get a ValueError exception. Let's try using simple quotes for enclosing the whole string and double quotes for the JSON keys and values:

band = '{"name": "Mando Diao", "genre": "rock and roll", "bid": 19383}'
json = simplejson.loads(band)

And there you go, no error now:

print json
{u'genre': u'rock and roll', u'bid': 19383, u'name': u'Mando Diao'}

I find a little strange that this problem happens as I've been using either type of quotes for most of my Python coding. Had you noticed this problem? I've checked simplejson's documentation and could not find anything related to this.

One last word, the new Python 2.6 includes a json module, which is a refactored simplejson. Neat.

Trackback URL for this post:

http://www.ventanazul.com/webzine/trackback/123

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.

More information about formatting options