Search code examples
javascriptarraysjsonxmlhttprequest

XMLHttpRequest to JSON file won't return all descendent objects, only 32


I have a JSON file with nested arrays and objects and I am creating an XMLHttpRequest to access the data.

It is working for the most part, I am able to get most of the data from the different JSON objects, but on all of them, it only loops through 32 of each object's child objects.

Here is a sample of the data in the routes.json file:

{
  "Routes": [
    {
      "name": "Route 1",
      "locations": [
        {
          "Name": "ABC Store",
          "Address": "123 W 456 S"
        },
        {
          "Name": "XYZ Store",
          "Address": "321 E 654 N"
        },
        {
          ... (66 other locations)
        }
      ]
    },
    {
      ... (more routes)
    }
  ]
}

Here is how I am calling the data from my main index.php file:

var request = new XMLHttpRequest();
request.open("GET", "routes.json", false);
request.send(null);
var route_data = JSON.parse(request.responseText);

var allRoutes = route_data.Routes;

for (var key1 in allRoutes) {

  var locations = allRoutes[key1].locations;
  for (var key2 in locations) {
    //I forgot to put this line in, and it was causing the problem
    if (!allRoutes.hasOwnProperty(key2)) continue;

    // Repeat the Route Name for each location to get a count
    console.log("Route Name: "+allRoutes[key1].Name);
    // Repeats only 32 times max when there are really 66+ in most of them

  }

}

Is there a limit on looping through arrays or objects from requested JSON data?


Solution

  • Well definitely not, there can't any limit to parsing.

    Check this fiddle

    Check sample data based on your format

    https://api.myjson.com/bins/8w3bs
    

    Look for console, it would print the loops properly with more than 32 items.