0

I'm using Wikipedia API to get an article summary. The data is nested within a variable page ID key unique to each article.

How do I extract the extract key if I do not initially have the value of the pages key?

Example API response for 'Stack Overflow':

{
"query": {
    "pages": {
        "21721040": {
            "pageid": 21721040, # this is a unique key value for each article
            "ns": 0,
            "title": "Stack Overflow",
            "extract": "Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky, as a more open alternative to earlier Q&A sites such as Experts Exchange. The name for the website was chosen by voting in April 2008 by readers of Coding Horror, Atwood's popular programming blog.\nIt features questions and answers on a wide range of topics in computer programming. The website serves as a platform for users to ask and answer questions, and, through membership and active participation, to vote questions and answers up or down and edit questions and answers in a fashion similar to a wiki or Digg. Users of Stack Overflow can earn reputation points and \"badges\"; for example, a person is awarded 10 reputation points for receiving an \"up\" vote on an answer given to a question, and can receive badges for their valued contributions, which represents a kind of gamification of the traditional Q&A site or forum. All user-generated content is licensed under a Creative Commons Attribute-ShareAlike license. Questions are closed in order to allow low quality questions to improve. Jeff Atwood stated in 2010 that duplicate questions are not seen as a problem but rather they constitute an advantage if such additional questions drive extra traffic to the site by multiplying relevant keyword hits in search engines.\nAs of April 2014, Stack Overflow has over 2,700,000 registered users and more than 7,100,000 questions. Based on the type of tags assigned to questions, the top eight most discussed topics on the site are: Java, JavaScript, C#, PHP, Android, jQuery, Python and HTML."
            }
        }
    }
}

Update: Solution based on Uri's response...

key = response['query']['pages'].keys          # => ["21721040"]
response['query']['pages'][key[0]]['extract']  # data
3
  • 1
    response['query']['pages'].values.first['pageid'] ? Commented Feb 9, 2016 at 10:28
  • The 'pages' key value changes with different article queries (i.e. 21721040 is only pertinent to the 'Stack Overflow' query) Commented Feb 9, 2016 at 10:30
  • 2
    You want to extract the page in ruby or in javascript? Commented Feb 9, 2016 at 10:32

2 Answers 2

2

You can look at the keys of the hash:

response['query']['pages'].keys
# => ["21721040"]
Sign up to request clarification or add additional context in comments.

Comments

0

If you're using Ruby 2.3, you could go for a #dig:

Get the value using extracting the key first:

key = response.dig('query', 'pages')&.keys.first
# => "21721040"
response.dig('query', 'pages', key, 'extract')
# => "Stack Overflow is a privately held website..."

Grab the extract value directly:

response.dig('query', 'pages')&.values.dig(0, 'extract')
# => "Stack Overflow is a privately held website..."

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.