We help IT Professionals succeed at work.
Get Started
Private
Troubleshooting Question

JavaScript loop not showing first record from JSON Array

Mike Assad
Mike Assad asked
on
36 Views
Last Modified: 2021-01-21
I am trying to get this script to show a list of values from a JSON feed. It does not work when there is only one record in the feed. It only works if there are two records in the feed. What am I doing wrong?

The Script
setInterval(function(){
  var xhr = new XMLHttpRequest();
  xhr.onload = function() {
    responseObject = JSON.parse(xhr.responseText);
    var newContent ='<table class="table table-sm table-striped m-0">'
    +'<thead>'
    +'<tr>'
    +'<th scope="col">Spot</th>'
    +'<th scope="col">Caller ID</th>'
    +'<th scope="col">Parked By</th>'
    +'<th scope="col">Duration</th>'
    +'</tr>'
    +'</thead>'
    +'<tbody>';
    if (responseObject.parked_calls.parked_call === undefined) {
      newContent += '<tr><td colspan="4">' +' No calls are parked.' + '</td></tr>';
    }
    else {
      for (var i = 0;i < responseObject.parked_calls.parked_call.length; i++) {
          newContent += '<tr>';
          newContent += '<td>' + responseObject.parked_calls.parked_call[i].lotnum + '</td>';
          newContent += '<td>' + responseObject.parked_calls.parked_call[i].from_caller_id_name + '<br><small>' + responseObject.parked_calls.parked_call[i].from_caller_id_number + '</small></td>';
          newContent += '<td>' + responseObject.parked_calls.parked_call[i].parked_by_extension + '</td>';
          newContent += '<td>' + responseObject.parked_calls.parked_call[i].duration + '</td>';
          newContent += '</tr>';
      }
    }
    newContent +='</tbody></table>';
    $('.parked-calls').html(newContent);
  };
  xhr.open('GET', 'URL', true);
  xhr.send(null);
}, 1000);
Singular Array
{
   "parking_lot_stop":"709",
   "timeout":"120",
   "parked_calls":{
      "parked_call":{
         "parked_by_extension":"132",
         "lotnum":"701",
         "to_caller_id_number":"",
         "duration":"31",
         "current_call_id":"",
         "from_caller_id_name":"<unknown>",
         "to_caller_id_name":"",
         "from_caller_id_number":""
      }
   },
   "date_created":"",
   "parking_lot_start":"",
   "account_id":""
}
Plural Array
{
   "parking_lot_stop":"709",
   "timeout":"120",
   "parked_calls":{
      "parked_call":[
         {
            "parked_by_extension":"132",
            "lotnum":"701",
            "to_caller_id_number":"",
            "duration":"",
            "current_call_id":"",
            "from_caller_id_name":"",
            "to_caller_id_name":"",
            "from_caller_id_number":""
         },
         {
            "parked_by_extension":"132",
            "lotnum":"702",
            "to_caller_id_number":"",
            "duration":"95",
            "current_call_id":"",
            "from_caller_id_name":"<unknown>",
            "to_caller_id_name":"",
            "from_caller_id_number":""
         }
      ]
   },
   "date_created":"",
   "parking_lot_start":"",
   "account_id":""
}

Comment
Watch Question
HainKurtSr. System Analyst
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 4 Answers and 14 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE