This is probably the difference between a JavaScript object and a JavaScript array. Objects are more like hash tables where keys are not sorted in any particular order, while arrays are linear collections of values.
At the end, make sure you encode the array, not the object. Check the final encoded JSON, and if your collection of objects is surrounded by {} instead of [], it will be encoded as an object, not an array.
You may have a problem, since it looks like you are trying to access objects by ID number and that the index you want these objects to occupy in the final array is another problem, because you probably don't want an array with 40,000 entries when you only save a small amount of values.
If you just want to iterate over objects, you have to make sure that you encode the array instead of the object. If you want to access objects by a specific identifier, you probably have to sort the objects on the client side (i.e., have the object from the JSON response, and then create another array and sort these objects in it so that you can have sorted objects and still have access to them by id).
With Google, you can easily find efficient sorting algorithms (or use one of them from ELCas).
source share