Can't access a propertu in a JSON object

can synonym
can definition
can verb
cant
can.'' acronym
can band
can't
could

I want to access a property in a JSON object i'm getting from the wikipedia API. You can see the object here

{
  "batchcomplete": "",
  "query": {
    "normalized": [
      {
        "from": "baguette",
        "to": "Baguette"
      }
    ],
    "pages": {
      "274152": {
        "pageid": 274152,
        "ns": 0,
        "title": "Baguette",
        "extract": "Une baguette est, dans son sens premier, une tige de bois, de métal, ou de divers autres matériaux ; le mot peut désigner aussi d’autres concepts, notamment par métonymie.\n\n"
      }
    }
  }
}

What I want is : query -> pages -> 274152 -> extract

I can access this value but the problem is I need to know the '274152' value which changes for every object so I'd like a solution to access it which would work for any number.

So the farthest I can go is obj.query.pages

Any idea ? Thanks

You can use:

var keys = Object.keys(obj.query.pages);

To get array of properties defined in the pages object. Then you can basically do obj.query.pages[keys[i]] to access i-th item in the pages object, for example:

obj.query.pages[keys[0]].extract

Alternatively you can loop over the properties like this:

for (var key in obj.query.pages) {
    if (obj.query.pages.hasOwnProperty(key)) {
        var item = obj.query.pages[key];
        //do something with item
        //item.extract...
    }
}

Can, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

Assuming the given JSON is a variable data

let uniqueID = Object.keys(data.query.pages)[0];
data.query.pages[uniqueID].extract

Will give you the desired result.

Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

If you just care about the 'extract' part, you can try this:

Object.values(obj.query.pages).map(item => item.extract)

To get a list of all the values

can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most  can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

CAN, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

The Official CAN / Spoon Records Website, can definition: The definition of a can is a container normally made of metal with a lid. (noun) An example of can is what tuna comes in. A Controller Area Network (CAN bus) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other's applications without a host computer. It is a message-based protocol , designed originally for multiplex electrical wiring within automobiles to save on copper, but can also be used in many other

Can, can. (kæn). n. 1. a container, esp for liquids, usually of thin sheet metal: a petrol can;  Can (stylised as CAN) was a German experimental rock band formed in Cologne in 1968 by the core quartet of Holger Czukay (bass, tape editing), Irmin Schmidt (keyboards), Michael Karoli (guitar), and Jaki Liebezeit (drums).

Comments
  • i think, it will return an array.