0

Good day, I'm trying to run a simple getJSON to get information from data json, almost done but when running, get undefined in html

Here's my jquery :

$(document).ready( function() {
  $.getJSON("/airport.json?code=bgw", function(data) {
    $('#stage').html('<p> Name: ' + data.result.request.code + '</p>');
    $.each(data.result.response.airport.pluginData.schedule.arrivals.data, function() {
      $("ul").append("<li>Name: "+this['flight.status.text']+"</li><br />");
    });
  });
});

data json :

`  {
  "result": {
    "response": {
      "airport": {
        "pluginData": {
          "schedule": {
            "arrivals": {
              "data": [
                {
                  "flight": {
                    "status": {
                      "live": true,
                      "text": "Estimated 13:44",
                      "icon": "green",
                      "estimated": null,
                      "ambiguous": false
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
`  

Any ideas on what I might be doing wrong?

3
  • As given in your data json, There's no [request] key in your [data.result] object property. Is this your complete version of your json? Commented Jun 11, 2017 at 11:22
  • Your data.json file is enclosed in back-ticks Commented Jun 11, 2017 at 11:24
  • yes is complete version Commented Jun 11, 2017 at 11:25

2 Answers 2

1

You're accessing the nested object as this['flight.status.text']. I believe you want to do this.flight.status.text. See the difference below

var data = [{
  test: {
    deep: {
      nested: {
        object: 1
      }
    }
  }
}, {
  test: {
    deep: {
      nested: {
        object: 2
      }
    }
  }
}];

console.log("Not working");
$.each(data, function() {
  console.log(this['test.deep.nested.object']);
});

console.log("--------------");

console.log("Working");
$.each(data, function() {
  console.log(this.test.deep.nested.object);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Also, as per your your JSON, data.result.request.code doesn't exist.

Sign up to request clarification or add additional context in comments.

Comments

1

Use this.flight.status.text to display the text like below

 $(document).ready( function() {
 data={
  "result": {
    "response": {
      "airport": {
        "pluginData": {
          "schedule": {
            "arrivals": {
              "data": [
                {
                  "flight": {
                    "status": {
                      "live": true,
                      "text": "Estimated 13:44",
                      "icon": "green",
                      "estimated": null,
                      "ambiguous": false
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
};
                      $('#stage').html('<p> Name: ' + 'bgw' + '</p>');  $.each(data.result.response.airport.pluginData.schedule.arrivals.data, function(){
                      $("ul").append("<li>Name: "+this.flight.status.text+"</li><br />");
                       });


 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stage"></div>
<ul></ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.