Trouble accessing json - WikiViewer

Trouble accessing json - WikiViewer
0.0 0

#1

I am able to receive the json data from the api, but I’m not sure how to actually access the info inside the pages, like the title or the extract, without going through each one at a time. Because they each have a different page number.

My javascript:


#2

Using your Wikipedia endpoint and the input of ‘hello’, I get the following response:

{
  "warnings": {
    "main": {
      "*": "Unrecognized parameter: pilimit."
    }
  },
  "batchcomplete": "",
  "continue": {
    "gsroffset": 10,
    "continue": "gsroffset||"
  },
  "query": {
    "pages": {
      "13834": {
        "pageid": 13834,
        "ns": 0,
        "title": "\"Hello, World!\" program",
        "index": 3,
        "extract": "A \"Hello, World!\" program is a computer program that outputs or displays \"Hello, World!\" to a user. Being a very simple program in most programming languages, it is often used to illustrate the basic syntax of a programming language for a working program."
      },
      "6710844": {
        "pageid": 6710844,
        "ns": 0,
        "title": "Hello",
        "index": 1,
        "extract": "Hello is a salutation or greeting in the English language. It is first attested in writing from 1826."
      },
      "7024876": {
        "pageid": 7024876,
        "ns": 0,
        "title": "Hello, Dolly! (film)",
        "index": 8,
        "extract": "Hello, Dolly! is a 1969 American romantic comedy musical film based on the Broadway production of the same name."
      },
      "2444754": {
        "pageid": 2444754,
        "ns": 0,
        "title": "Hello, Frisco, Hello",
        "index": 9,
        "extract": "Hello, Frisco, Hello (1943) is a musical film starring Alice Faye, John Payne, Lynn Bari, and Jack Oakie. The film was made in Technicolor and released by 20th Century-Fox."
      },
      "48324759": {
        "pageid": 48324759,
        "ns": 0,
        "title": "Hello (Adele song)",
        "index": 7,
        "extract": "\"Hello\" is a song by English singer Adele, released on 23 October 2015 by XL Recordings as the lead single from her third studio album, 25 (2015). Adele co-wrote the song with her producer, Greg Kurstin."
      },
      "15867959": {
        "pageid": 15867959,
        "ns": 0,
        "title": "Hello Brother",
        "index": 10,
        "extract": "Hello Brother may refer to:"
      },
      "42663426": {
        "pageid": 42663426,
        "ns": 0,
        "title": "Hello Hello",
        "index": 2,
        "extract": "Hello Hello may refer to:"
      },
      "54295": {
        "pageid": 54295,
        "ns": 0,
        "title": "Hello Kitty",
        "index": 4,
        "extract": "Hello Kitty (Japanese: \u30cf\u30ed\u30fc\u30fb\u30ad\u30c6\u30a3, Hepburn: Har\u014d Kiti) (full name: Kitty White (\u30ad\u30c6\u30a3\u30fb\u30db\u30ef\u30a4\u30c8, Kiti Howaito)) is a fictional character produced by the Japanese company Sanrio, created by Yuko Shimizu and currently designed by Yuko Yamaguchi. According to her backstory, she is a perpetual 3rd grade student who lives outside London."
      },
      "1379058": {
        "pageid": 1379058,
        "ns": 0,
        "title": "Hello Muddah, Hello Fadduh (A Letter from Camp)",
        "index": 5,
        "extract": "\"Hello Muddah, Hello Fadduh (A Letter from Camp)\" is a novelty song by Allan Sherman and Lou Busch, based on letters of complaint Allan received from his son Robert while Robert attended Camp Champlain in Westport, New York."
      },
      "54077698": {
        "pageid": 54077698,
        "ns": 0,
        "title": "Hello Neighbor",
        "index": 6,
        "extract": "Hello Neighbor is a survival horror stealth game developed by Dynamic Pixels and published by tinyBuild. The goal of the game is for the player to successfully sneak into the basement of their neighbor's house to uncover a secret."
      }
    }
  },
  "limits": {
    "extracts": 20
  }
}

By now you should have already learned about how to access nested properties in objects. For example, the heart of the data is located in the pages property (seen below):

    "pages": {
      "13834": {
        "pageid": 13834,
        "ns": 0,
        "title": "\"Hello, World!\" program",
        "index": 3,
        "extract": "A \"Hello, World!\" program is a computer program that outputs or displays \"Hello, World!\" to a user. Being a very simple program in most programming languages, it is often used to illustrate the basic syntax of a programming language for a working program."
      },
      "6710844": {
        "pageid": 6710844,
        "ns": 0,
        "title": "Hello",
        "index": 1,
        "extract": "Hello is a salutation or greeting in the English language. It is first attested in writing from 1826."
      },
      "7024876": {
        "pageid": 7024876,
        "ns": 0,
        "title": "Hello, Dolly! (film)",
        "index": 8,
        "extract": "Hello, Dolly! is a 1969 American romantic comedy musical film based on the Broadway production of the same name."
      },
      "2444754": {
        "pageid": 2444754,
        "ns": 0,
        "title": "Hello, Frisco, Hello",
        "index": 9,
        "extract": "Hello, Frisco, Hello (1943) is a musical film starring Alice Faye, John Payne, Lynn Bari, and Jack Oakie. The film was made in Technicolor and released by 20th Century-Fox."
      },
      "48324759": {
        "pageid": 48324759,
        "ns": 0,
        "title": "Hello (Adele song)",
        "index": 7,
        "extract": "\"Hello\" is a song by English singer Adele, released on 23 October 2015 by XL Recordings as the lead single from her third studio album, 25 (2015). Adele co-wrote the song with her producer, Greg Kurstin."
      },
      "15867959": {
        "pageid": 15867959,
        "ns": 0,
        "title": "Hello Brother",
        "index": 10,
        "extract": "Hello Brother may refer to:"
      },
      "42663426": {
        "pageid": 42663426,
        "ns": 0,
        "title": "Hello Hello",
        "index": 2,
        "extract": "Hello Hello may refer to:"
      },
      "54295": {
        "pageid": 54295,
        "ns": 0,
        "title": "Hello Kitty",
        "index": 4,
        "extract": "Hello Kitty (Japanese: \u30cf\u30ed\u30fc\u30fb\u30ad\u30c6\u30a3, Hepburn: Har\u014d Kiti) (full name: Kitty White (\u30ad\u30c6\u30a3\u30fb\u30db\u30ef\u30a4\u30c8, Kiti Howaito)) is a fictional character produced by the Japanese company Sanrio, created by Yuko Shimizu and currently designed by Yuko Yamaguchi. According to her backstory, she is a perpetual 3rd grade student who lives outside London."
      },
      "1379058": {
        "pageid": 1379058,
        "ns": 0,
        "title": "Hello Muddah, Hello Fadduh (A Letter from Camp)",
        "index": 5,
        "extract": "\"Hello Muddah, Hello Fadduh (A Letter from Camp)\" is a novelty song by Allan Sherman and Lou Busch, based on letters of complaint Allan received from his son Robert while Robert attended Camp Champlain in Westport, New York."
      },
      "54077698": {
        "pageid": 54077698,
        "ns": 0,
        "title": "Hello Neighbor",
        "index": 6,
        "extract": "Hello Neighbor is a survival horror stealth game developed by Dynamic Pixels and published by tinyBuild. The goal of the game is for the player to successfully sneak into the basement of their neighbor's house to uncover a secret."
      }
    }

Assuming the response had been assigned to a variable called data, then to access the pageID (needed for linking to the full page), the title, and the extract (summary), I could use the following:

var pages = data.query.pages;
for (page in pages) {
  let {pageid, title, extract} = pages[page]; // Object destructuring
  console.log(pageid);
  console.log(title);
  console.log(extract);
  console.log();
}