QuirkyBit
QuirkyBit

Reputation: 762

Get all elements from JSON into a list in python

I have the following JSON response from https://en.wikipedia.org/w/api.php?action=query&titles=Web%20Bot&prop=links&pllimit=max&format=json

{'batchcomplete': '', 'query': {'pages': {'24482718': {'pageid': 24482718, 'ns': 0, 'title': 'Web Bot', 'links': [{'ns': 0, 'title': '2004 Indian Ocean earthquake'}, {'ns': 0, 'title': '2012 phenomenon'}, {'ns': 0, 'title': 'Algorithm'}, {'ns': 0, 'title': 'Barack Obama'}, {'ns': 0, 'title': 'Daily Telegraph'}, {'ns': 0, 'title': 'Doomsday 2012'}, {'ns': 0, 'title': "Earth's magnetic field"}, {'ns': 0, 'title': 'El Día (La Plata)'}, {'ns': 0, 'title': 'Global Consciousness Project'}, {'ns': 0, 'title': 'Google Flu Trends'}, {'ns': 0, 'title': 'Google Trends'}, {'ns': 0, 'title': 'History (U.S. TV channel)'}, {'ns': 0, 'title': 'Hurricane Katrina'}, {'ns': 0, 'title': 'Internet bot'}, {'ns': 0, 'title': 'Internet bots'}, {'ns': 0, 'title': 'Iran'}, {'ns': 0, 'title': 'Israel'}, {'ns': 0, 'title': 'Northeast Blackout of 2003'}, {'ns': 0, 'title': 'Nostradamus Effect'}, {'ns': 0, 'title': 'Pacific Northwest'}, {'ns': 0, 'title': 'Postdiction'}, {'ns': 0, 'title': 'Predictive analytics'}, {'ns': 0, 'title': 'Pseudoscientific'}, {'ns': 0, 'title': 'Seeking Alpha'}, {'ns': 0, 'title': 'The Epoch Times'}, {'ns': 0, 'title': 'The Globe and Mail'}, {'ns': 0, 'title': 'The Jerusalem Post'}, {'ns': 0, 'title': 'Vancouver, British Columbia'}, {'ns': 4, 'title': 'Wikipedia:Verifiability'}, {'ns': 14, 'title': 'Category:Articles with failed verification from August 2015'}, {'ns': 14, 'title': 'Category:Use dmy dates from January 2012'}]}}}, 'limits': {'links': 500}}

I would like to get all the titles into a list as so [2004 Indian Ocean earthquake, 2012 phenomenon, Barack Obama ... ]

I have done the following:

json_doc = urllib.request.urlopen(url).read().decode(encoding="utf-8", errors="ignore")
list_of_titles = []
page_id = parsed['query']['pages']
first = next(iter(page_id.values()))
for element in first['links']:
    list_of_elements.append(element['title'])

What would be the easiest way to do so without ugly and nested loops?

Upvotes: 1

Views: 159

Answers (1)

Selcuk
Selcuk

Reputation: 59184

Your code looks fine to me but you can shorten it a little bit by using a list comprehension:

...
list_of_elements = []
for page in parsed["query"]["pages"].values():
    list_of_elements.extend([link["title"] for link in page["links"]])

Upvotes: 1

Related Questions