Converting JSON into Python dict

68,373

Solution 1

The string you show is not a JSON-coded object (eqv to a Python dict) — more like an array (eqv to a list) without brackets and with a stray extra comma at the end. So (using simplejson for version portability — the standard library's json in 2.6 is fine too of course!-):

>>> import simplejson
>>> js = "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"},{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
>>> simplejson.loads('[%s]' % js[:-1])
[{'description': 'fdsafsa', 'order': '1', 'place': '22 Plainsman Rd, Mississauga, ON, Canada', 'lat': 43.596917500000004, 'lng': -79.724874400000004, 'locationDate': '03/24/2010'}, {'description': 'sadfdsa', 'order': '2', 'place': '50 Dawnridge Trail, Brampton, ON, Canada', 'lat': 43.730477399999998, 'lng': -79.805543499999999, 'locationDate': '03/26/2010'}]

If you really want a dict you'll have to specify how to treat these two unnamed items, i.e., what arbitrary keys you want to slap on them...?

Solution 2

  • Use the json module for loading JSON. (Pre-2.6 use the third party simplejson module, which has the same exact API.)

    >>> import json
    >>> s = '{"foo": 6, "bar": [1, 2, 3]}'
    >>> d = json.loads(s)
    >>> print d
    {u'foo': 6, u'bar': [1, 2, 3]}
    
  • Your actual data cannot be loaded this way since it's actually two JSON objects separated by a comma and with a trailing comma. You'll need to separate them or otherwise deal with this.

    • Where did you get this string?

Solution 3

django.utils.simplejson.loads(someJson)
Share:
68,373

Related videos on Youtube

GrumpyCanuck
Author by

GrumpyCanuck

Long time web application programmer, now working for a social commerce company.

Updated on July 21, 2021

Comments

  • GrumpyCanuck
    GrumpyCanuck almost 3 years

    I've been searching around trying to find an answer to this question, and I can't seem to track it down. Maybe it's too late in the evening to figure the answer out, so I turn to the excellent readers here.

    I have the following bit of JSON data that I am pulling out of a CouchDB record:

    "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"},{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
    

    This data is stored inside a Python dict under the key 'locations' in a dict called 'my_plan'. I want to covert this data from CouchDB into a Python dict so I can do the following in a Django template:

    {% for location in my_plan.locations %}                                                           
    <tr>
        <td>{{ location.place }}</td>
        <td>{{ location.locationDate }}</td>
    </tr>
    
    {% endfor %}
    

    I've found lots of info on converting dicts to JSON, but nothing on going back the other way.

  • GrumpyCanuck
    GrumpyCanuck about 14 years
    Doesn't convert to a dict. I did try that ;)
  • GrumpyCanuck
    GrumpyCanuck about 14 years
    The actual error it gives is "Extra data: line 1 column 151 - line 1 column 304 (char 151 - 304)"
  • GrumpyCanuck
    GrumpyCanuck about 14 years
    Your solution worked perfectly. Thanks! I will fix the routine that generates that data before it goes into CouchDB to not append that extra comma. A bit of late-night coding sloppiness
  • Mike Graham
    Mike Graham about 14 years
    @GrumpyCanuck, Look closely. This isn't one object. It's an object then a comma then an object then a comma.
  • GrumpyCanuck
    GrumpyCanuck about 14 years
    That string is from data being generated by an application I am working on, sort of a location-and-date social app for people planning trips, pub crawls, etc
  • Alex Martelli
    Alex Martelli about 14 years
    @Grumpy, sure -- if I were you, I'd also put the brackets around the string in the DB, just to make sure it's valid JSON rather than "somewhat incomplete JSON" that the receiving code must complete.
  • GrumpyCanuck
    GrumpyCanuck about 14 years
    Yeah, I figured that out when I saw @Alex Martelli's answer
  • GrumpyCanuck
    GrumpyCanuck about 14 years
    I was doing that before, but cannot remember why I stopped doing it...late-night coding requires notes from now on I think
  • holms
    holms over 8 years
    why not using json module? simplejson is legacy which should be deprecated long time ago. Check answer below why :)