2

Thank you for taking the time to read my question! I am new to JavaScript and have been unable to find a solution to the following question:

How can I access the value of a property that is nested in a JSON object under a property whose name dynamically changes?

The JSON object:

{
  "batchcomplete": "",
  "query": {
    "normalized": [
      {
        "from": "Theodore_Rubin",
        "to": "Theodore Rubin"
      }
    ],
    "pages": {
      "11820415": {
        "pageid": 11820415,
        "ns": 0,
        "title": "Theodore Rubin",
        "contentmodel": "wikitext",
        "pagelanguage": "en",
        "pagelanguagehtmlcode": "en",
        "pagelanguagedir": "ltr",
        "touched": "2016-02-12T17:34:52Z",
        "lastrevid": 138813300,
        "length": 34,
        "redirect": "",
        "new": "",
        "fullurl": "https://en.wikipedia.org/wiki/Theodore_Rubin",
        "editurl": "https://en.wikipedia.org/w/index.php?title=Theodore_Rubin&action=edit",
        "canonicalurl": "https://en.wikipedia.org/wiki/Theodore_Rubin"
      }
    }
  }
}

I am trying to assign the value of the property fullurl to a variable. I understand that I can use a combination of bracket and dot notation to access the fullurl such as query.pages["2123909"].fullurl. The problem is that the property name ["2123909"] changes with each JSON request.

How can I access the value of fullurl property without knowing the name of the property that it is nested in?

Thank you very much for your help!

  • If there is always only one property, see [JavaScript: Get first and only property name of object](http://stackoverflow.com/q/6765864/218196) – Felix Kling Nov 02 '16 at 19:19

2 Answers2

2

If there is only one key, you could get the first element with Object.keys of data.query.pages.

var data = {
        "batchcomplete": "",
        "query": {
            "normalized": [
              {
                  "from": "Theodore_Rubin",
                  "to": "Theodore Rubin"
              }
            ],
            "pages": {
                "11820415": {
                    "pageid": 11820415,
                    "ns": 0,
                    "title": "Theodore Rubin",
                    "contentmodel": "wikitext",
                    "pagelanguage": "en",
                    "pagelanguagehtmlcode": "en",
                    "pagelanguagedir": "ltr",
                    "touched": "2016-02-12T17:34:52Z",
                    "lastrevid": 138813300,
                    "length": 34,
                    "redirect": "",
                    "new": "",
                    "fullurl": "https://en.wikipedia.org/wiki/Theodore_Rubin",
                    "editurl": "https://en.wikipedia.org/w/index.php?title=Theodore_Rubin&action=edit",
                    "canonicalurl": "https://en.wikipedia.org/wiki/Theodore_Rubin"
                }
            }
        }
    };

console.log(data.query.pages[Object.keys(data.query.pages)[0]].fullurl);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

If the object has always only one property name, get the property name and access the property via bracket notation:

var key = // see https://stackoverflow.com/q/6765864/218196
query.pages[key].fullurl

If there are more more than one property, iterate over the object, e.g. via for...in. See Access / process (nested) objects, arrays or JSON for that.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143