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.
asked Jun 20, 2011 at 10:27
user179169
2 Answers 2
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']
answered Jun 20, 2011 at 10:31
Artsiom Rudzenka
29.3k5 gold badges36 silver badges53 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Artsiom Rudzenka
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}]?
That list contains 2 separate dicts. Iterate over the list directly.
answered Jun 20, 2011 at 10:31
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
2 Comments
Ignacio Vazquez-Abrams
for foo in [1, 2, 3]: do_something(foo)lang-py
{in your JSON. (As if you'd be missing a bracket in Python.)