1

This is probably a really stupid question, but I'm getting a JSON object returned by an API call and I can't for the life of me figure out how to get the values out of it. After I make the call, I pretty print the JSON object and it shows the following:

 [
  {
    "link_request": {
      "success": true,
      "link": "https://www.blah.com"
    },
    "errors": null
  }
]

I want to be able to get at that link value. I've tried both of the following, but neither works.

var link = data.query.link;

var link = data['query']['link']

Any help? Thanks!

user2874270
  • 1,312
  • 2
  • 18
  • 31
  • The root value is an Array. You can access the Object within it using an index -- `data[0]`. And, the other Object within that by one of its properties -- `data[0].link_request`. Etc. ([Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json)) – Jonathan Lonowski Aug 10 '15 at 04:17
  • Also keep in mind, if you are getting a string, parse it into an object via `JSON.parse` and then use access its properties. – deleted user Aug 10 '15 at 04:25

2 Answers2

3

Here it is

obj[0].link_request.link
Suraj Rawat
  • 3,685
  • 22
  • 33
0

Use the below code to get link:

jsonArray = [ {
    "link_request": {
      "success": true,
      "link": "https://www.blah.com"
    },
    "errors": null
    }
];
console.log(jsonArray[0].link_request.link)
Sumit
  • 83
  • 2
  • 11