7

From the docs: http://docs.python.org/library/json.html

>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]

I modified it like this:

>>> the_dump=json.dumps("['foo', {'bar':['baz', null, 1.0, 2]}]")
>>> the_load = json.loads(the_dump)
u"['foo', {'bar':['baz', null, 1.0, 2]}]"

Now it's a string. I want to do this: the_load[1]['bar'].

Can it be done this way? Where am I going wrong?

Why does this work?

>>> a= "[1,2,3]"
>>> json.loads(a)[0]
1
1
  • 1
    If you're interested in speed and security, I'd recommend installing the simplejson module yourself. Python's json module, as of 2.6, is an older version of simplejson that doesn't have all the speed and security improvements of the latest version. Commented Dec 21, 2011 at 17:59

1 Answer 1

12
>>> the_dump=json.dumps("['foo', {'bar':['baz', null, 1.0, 2]}]")

You're asking it to json encode a string, so it's not surprising that you get a string back when you decode. Try instead:

>>> the_dump=json.dumps(['foo', {'bar':['baz', None, 1.0, 2]}])
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.