Parsing random keys in Javascript

Good day,

I had a problem when i parsing this result from wikipedia.

"batchcomplete": "",
"query": {
    "pages": {
        "252408": {
            "pageid": 252408,
            "ns": 0,
            "title": "Bulacan",
            "extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"

but it creates a random number key "252408", i want to parse the "extract" key without declaring the random number key and extract key.

So, if the object under the "pages" key will always only have one key, and you don't know the value of it, you can use,

var randKeyObj = Object.keys(obj.query.pages)[0];

This will always get the first or only key of an object thats passed in.

0 Comment



Captcha image