2

I'm having a bit of an issue with parsing json with python using the json library. Here is the format of the json I'm trying to parse:

{'entry':[
    {

        JSON Data 1
    }, 

        JSON Data 2
    }
]}

And here is my Python:

for entry in response['entry'][0]:

    video['video_url'] = entry['id']['$t']
    video['published'] = entry['published']['$t']

I don't seem to be able to iterate over the two blocks of JSON with the above code, I only get the first block outputted for some reason.

Anybody have any ideas?? Thanks in advance.

1
  • You seem to be missing a { in your JSON. (As if you'd be missing a bracket in Python.) Commented Jun 20, 2011 at 10:31

2 Answers 2

1

If:

response = {'entry':[
    {

        JSON Data 1
    }, 
    {

        JSON Data 2
    }
]}

And:

response['entry'][0] == { JSON Data 1  }

Then:

for entry in response['entry']:

    video['video_url'] = entry['id']['$t']
    video['published'] = entry['published']['$t']

Or:

video = dict(zip(['video_url', 'published'], [entry['id']['$t'], entry['published']['$t']]) for entry in response['entry']
Sign up to request clarification or add additional context in comments.

3 Comments

Nope, that only returns the first block :-/ Thanks for your help
response['entry'][0] == { JSON Data 1 }, {JSON Data 2}
Ok, than could you please give us your input in details so we will able to understand what you really need? response['entry'][0] == { JSON Data 1 }, {JSON Data 2} or response['entry'][0] == [{ JSON Data 1 }, {JSON Data 2}]?
1

That list contains 2 separate dicts. Iterate over the list directly.

2 Comments

could you provide an example please?
for foo in [1, 2, 3]: do_something(foo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.