KAllmann
KAllmann

Reputation: 3

Accessing nested data in a Wiki API JSON response

I am still relatively new to all this, but I am trying to access Wikipedia's API in order to retrieve the value of "extract" and to append the text to an html element. The issue is that "pages" will change depending on user input. Is there a way to access the info given the the random number in the JSON response? *Edit- I am using Jquery/Javascript. this was the API request I sent: https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=Pug

{

"batchcomplete": "",
"query": {
    "normalized": [
        {
            "from": "pug",
            "to": "Pug"
        }
    ],
    "pages": {
        "21234727 (this number is will change/be random)": {
            "pageid": 21234727,
            "ns": 0,
            "title": "Pug",
            "extract": "The pug is a breed of dog with physically distinctive features of a wrinkly, short-muzzled face, and curled tail. The breed has a fine, glossy coat that comes in a variety of colours, most often fawn or black, and a compact square body with well-developed muscles.\nPugs were brought from China to Europe in the sixteenth century and were popularized in Western Europe by the House of Orange of the Netherlands, and the House of Stuart. In the United Kingdom, in the nineteenth century, Queen Victoria developed a passion for pugs which she passed on to other members of the Royal family.\nPugs are known for being sociable and gentle companion dogs. The American Kennel Club describes the breed's personality as \"even-tempered and charming\". Pugs remain popular into the twenty-first century, with some famous celebrity owners. A pug was judged Best in Show at the World Dog Show in 2004."
        }
    }
}

}

Upvotes: 0

Views: 112

Answers (1)

Bman70
Bman70

Reputation: 773

Extract is a hash inside of a randomly numbered hash, which is inside the pages hash, which is inside of the query hash. So you need the value of json->query->pages->random_number->extract.

This requires giving a name to the random number so you know how to refer to it each time. You didn't say what language you're using, but I'd try something like this in Perl (if you provide your language of choice someone else can show the correspondent operation):

foreach my $pagenum ( keys %{$json{'query'}{'pages'}}) {
  print "Random number is now called $pagenum\n"; 
my $extract = $json{'query'}{'pages'}{$pagenum}->{'extract'}; 
  print "Extract is $extract\n";
}

Result of printing $extract is: The pug is a breed of dog with physically distinctive features of a wrinkly... etc.

I got my son to translate the Perl operation into Ruby, so this works too. (Assuming the JSON is in a variable named json):

randnum = json['query']['pages'].keys[0]
extract_value = json['query']['pages'][randnum]['extract']
puts extract_value

UPDATE: (OP specified language)

I'm not great with Javascript, but this seems to work:

var extractValue = Object.values(your_json.query.pages)[0].extract; (where your_json is the JSON data you received).

Upvotes: 0

Related Questions