Mana Void
Mana Void

Reputation: 387

Python - How to skip a specific JSON element?

{
"query": {
    "pages": {
        "7412236": {
            "pageid": 7412236,
            "ns": 0,
            "title": "Steve Jobs",
            "extract": "<p><b>Steven Paul</b> \"<b>Steve</b>\" <b>Jobs</b> (/\u02c8d\u0292\u0252bz/; February 24, 1955\u00a0\u2013 October 5, 2011) was an American entrepreneur, marketer, and inventor, who was the co-founder (along with Steve Wozniak and Ronald Wayne), chairman, and CEO of Apple Inc. Through Apple, he is widely recognized as a charismatic pioneer of the personal computer revolution and for his influential career in the computer and consumer electronics fields, transforming \"one industry after another, from computers and smartphones to music and movies\". Jobs also co-founded and served as chief executive of Pixar Animation Studios; he became a member of the board of directors of The Walt Disney Company in 2006, when Disney acquired Pixar. Jobs was among the first to see the commercial potential of Xerox PARC's mouse-driven graphical user interface, which led to the creation of the Apple Lisa and, one year later, the Macintosh. He also played a role in introducing the LaserWriter, one of the first widely available laser printers, to the market.</p>\n<p>After a power struggle with the board of directors in 1985, Jobs left Apple and founded NeXT, a computer platform development company specializing in the higher-education and business markets. In 1986, he acquired the computer graphics division of Lucasfilm, which was spun off as Pixar. He was credited in <i>Toy Story</i> (1995) as an executive producer. He served as CEO and majority shareholder until Disney's purchase of Pixar in 2006. In 1996, after Apple had failed to deliver its operating system, Copland, Gil Amelio turned to NeXT Computer, and the NeXTSTEP platform became the foundation for the Mac OS X. Jobs returned to Apple as an advisor, and took control of the company as an interim CEO. Jobs brought Apple from near bankruptcy to profitability by 1998.</p>\n<p>As the new CEO of the company, Jobs oversaw the development of the iMac, iTunes, iPod, iPhone, and iPad, and on the services side, the company's Apple Retail Stores, iTunes Store and the App Store. The success of these products and services provided several years of stable financial returns, and propelled Apple to become the world's most valuable publicly traded company in 2011. The reinvigoration of the company is regarded by many commentators as one of the greatest turnarounds in business history.</p>\n<p>In 2003, Jobs was diagnosed with a pancreas neuroendocrine tumor. Though it was initially treated, he reported a hormone imbalance, underwent a liver transplant in 2009, and appeared progressively thinner as his health declined. On medical leave for most of 2011, Jobs resigned in August that year, and was elected Chairman of the Board. He died of respiratory arrest related to his tumor on October 5, 2011.</p>\n<p>Jobs received a number of honors and public recognition for his influence in the technology and music industries. He has been referred to as \"legendary\", a \"futurist\" or simply \"visionary\", and has been described as the \"Father of the Digital Revolution\", a \"master of innovation\", \"the master evangelist of the digital age\" and a \"design perfectionist\".</p>\n<p></p>"
        }
    }
}
}

So I fetch content provided by Wikipedia API in JSON format using the following code,

fetchedPage = urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&titles=Steve%20Jobs&format=json')
Json = json.load(fetchedPage)
content = Json['query']['pages']['7412236']['extract']
print content

It works fine for a single article as I can manually enter the article's "pageid". But on a more general basis I'll have to skip the "pageid" element so that I can fetch the content of any article directly.

In short, I want to achieve something like this,

content = Json['query']['pages'][//I dont care what's in this element]['extract']

How do I proceed?

Upvotes: 3

Views: 3144

Answers (2)

Maxime Lorant
Maxime Lorant

Reputation: 36161

You can access to the first element of a dictionary through dict.itervalues().next() without the key. The itervalues() returns a iterable of values in the dictionnary, and the next() returns the first element of this iterable.

fetchedPage = urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=query&
prop=extracts&exintro&titles=Steve%20Jobs&format=json')
Json = json.load(fetchedPage)
content = Json['query']['pages'].itervalues().next()['extract']
print content

Another way can be to get the first key of the dict with dict[dict.keys()[0]] if you feel it more explicit. In your example you'll have:

page_id = Json['query']['pages'].keys()[0]
content = Json['query']['pages'][page_id]['extract']

Upvotes: 2

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

This will build a list of "extracts" for all "pages" in your dict:

extracts = [p["extract"] for p in json["query"]["pages"].values()]

Upvotes: 2

Related Questions