1

I try to loop this json but it is not working.I want to loop through the shops but it is not working

The json:

{
 "type": "shops",
 "shops": {
 "1": {
 "id": "1",
 "name": "abcd",
 "open_time": "9AM",
 "closed_time": "9PM"
 },
 "2": {
 "id": "1",
 "name": "efgh",
 "open_time": "9AM",
 "closed_time": "9PM"
 }
 }
}

The script

$.getJSON( "simple.json", function( json ) {
 console.log( "JSON Data: " + (json) );
 for(var i = 0; i < json.shops.length; i++){
 console.log(json[i].shops.name);
 }
 });

This doesn't give me any results

asked Sep 28, 2015 at 14:26
8
  • 6
    shops is not an array. It is an object. stackoverflow.com/questions/684672/… Commented Sep 28, 2015 at 14:27
  • 1
    Can you please console.log(json); and tell me what it is returning? Commented Sep 28, 2015 at 14:27
  • it gives me [object Object] Commented Sep 28, 2015 at 14:28
  • As per the above JSON doc you provided that is not an array a single json document. You don't have to loop on that Commented Sep 28, 2015 at 14:29
  • 1
    If you are the one generating the JSON, perhaps you should fix it to be an array and not an object. Commented Sep 28, 2015 at 14:39

3 Answers 3

4

json.shops isn't an array, it's an object. You can use a for..in loop or $.each

Using $.each

$.getJSON( "simple.json", function( json ) {
 console.log( "JSON Data: " + (json) );
 $.each(json.shops, function(key, shop){
 //key will be "1", "2"...n
 console.log(shop.name, shop.id);
 });
 });

Using for..in

 for(var shop in json.shops){
 //shop will be "1", "2"...n
 if (json.shops.hasOwnProperty(shop))
 console.log(json.shops[shop].name);
 }
answered Sep 28, 2015 at 14:36
Sign up to request clarification or add additional context in comments.

8 Comments

$.each will return property names here, value.name and value.id are undefined.
Hehe, just kidding ;) But you did make me retest it 3 times!
@MarcosCasagrande can i use just for loop without using for in?
@user3475082 You can't loop through an object with a normal for loop. You have to use the for in statement. You can just use jQuery.each if you don't like the for in!
@MarcosCasagrande when we are dealing with json is this correct?..i mean using objects without arrays?
|
1

As shown in the comments, shops is an object, not an array. You can loop through all the properties of a javascript object using:

$.getJSON( "simple.json", function( json ) {
 console.log( "JSON Data: " + (json) );
 for(var key in json.shops){
 if (!json.shops.hasOwnProperty(key)) continue;
 console.log(json.shops[key]);
 }
 });
answered Sep 28, 2015 at 14:35

Comments

0

json.shops.length is undefined, because shops is an object and not an array and doesn't (in this case) have a "length" property. but every JavaScript object can have properties, and those property names can be retrieved as a list, which you can then iterate over:

for(var i = 0; i < Object.keys(json.shops).length; i++){
 console.log(json.shops[Object.keys(json.shops)][i].name);
}

or, alternately, fetch the object's keys as an array and use the forEach function built in to arrays:

Object.keys(json.shops).forEach(function(element, arrayIndex, array) {
 console.log(array[element].name);
});
answered Sep 28, 2015 at 14:33

1 Comment

You first code section is wrong - Object.keys(json.shops)[i] will return a string key, not an object.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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