-2

For some reason I am unable to pull values from a JSON array using jQuery. I have tried many things but it always shows as 'undefined' in the console.

Screenshot attached. Any help would be greatly appreciated.

Screenshot of JSON output

Edit: currently non working code below:

$(function(){
        $.getJSON('MyJSONPathURL', function(data) {
        var obj = (data);
        console.log(obj);
        var test = (data["0"].description)
        console.log(test);
    });
});

Whatever I do it seems the console.log result of my test variable is 'Undefined'.

Flowbyte
  • 1
  • 1

1 Answers1

0

You should be able to use a few different strategies to see specific JSON values. You are not alone in finding this tricky though. Because JSON is not actual Javascript, it does not behave in the ways you may be used to interacting with JS.

https://weblog.west-wind.com/posts/2011/apr/01/displaying-json-in-your-browser

Depending on whatever you are using for data binding, there are also additional tricks you can use. Knockout.js for instance has a feature like this:

<pre data-bind="text: ko.toJSON($data,null,2)"></pre> 

That you can use. Depending on where you put the closing tag that you can actually get it to put all the content into your browser, visibly.

I've also heard of a JSON.stringify() function but haven't personally given that one a try yet.

Joy_TK
  • 11
  • 3