Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a json file returned on my javascript code. The file looks like this :

{
    "data": [
        {
            "id": "594984240522886",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Ducks",
            "description": "ducks",
            "link": "http://www.facebook.com/album.php?fbid=594984240522886&id=593959083958735&aid=1073741834",
            "cover_photo": "594984260522884",
            "count": 4,
            "type": "normal",
            "created_time": "2013-06-13T15:12:22+0000",
            "updated_time": "2013-06-13T15:12:40+0000",
            "can_upload": false
        },
        {
            "id": "593963787291598",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Profile Pictures",
            "link": "http://www.facebook.com/album.php?fbid=593963787291598&id=593959083958735&aid=1073741832",
            "cover_photo": "593963797291597",
            "count": 1,
            "type": "profile",
            "created_time": "2013-06-11T16:52:29+0000",
            "updated_time": "2013-06-11T16:52:31+0000",
            "can_upload": false
        },
        {
            "id": "593963467291630",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Goats",
            "description": "goats",
            "link": "http://www.facebook.com/album.php?fbid=593963467291630&id=593959083958735&aid=1073741831",
            "cover_photo": "593963477291629",
            "count": 7,
            "type": "normal",
            "created_time": "2013-06-11T16:51:56+0000",
            "updated_time": "2013-06-11T16:52:02+0000",
            "can_upload": false
        },
        {
            "id": "593962700625040",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Dogs",
            "description": "dogs",
            "link": "http://www.facebook.com/album.php?fbid=593962700625040&id=593959083958735&aid=1073741830",
            "cover_photo": "593962710625039",
            "count": 10,
            "type": "normal",
            "created_time": "2013-06-11T16:50:27+0000",
            "updated_time": "2013-06-11T16:50:37+0000",
            "can_upload": false
        },
        {
            "id": "593961937291783",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Cows",
            "description": "Cows",
            "link": "http://www.facebook.com/album.php?fbid=593961937291783&id=593959083958735&aid=1073741829",
            "cover_photo": "593961983958445",
            "count": 5,
            "type": "normal",
            "created_time": "2013-06-11T16:48:26+0000",
            "updated_time": "2013-06-11T16:49:32+0000",
            "can_upload": false
        }
    ],
    "paging": {
        "cursors": {
            "after": "NTkzOTYxOTM3MjkxNzgz",
            "before": "NTk0OTg0MjQwNTIyODg2"
        }
    }
}

I would like to loop inside the "data" and see how many different data elements exist(as you see each element has an id , from , name , description..) . How can i do that with javascript?

share|improve this question
    
What have you tried so far ? –  Ani Jun 17 '13 at 16:13
    
google.fr/search?q=manipulate+json –  pataluc Jun 17 '13 at 16:14
    
Look at the length property of the "data" array. –  Elias Zamaria Jun 17 '13 at 16:16
    
What is the main json object name ? you can loop thru using for loop this : for(i=0; i<jsonObj.data.length; i++) –  Ani Jun 17 '13 at 16:17
    
Thank you very much mikez302. Thats indeed what i needed. I was thinking about making foreach loops but this one is 100 times cleaner. –  Jonh Smid Jun 17 '13 at 16:17

1 Answer 1

up vote 1 down vote accepted

You can try the following code:

for(i=0;json.data.length;i++){
    var element = json.data[i];
}

or also in this other way:

for (i in json.data) {
    if (json.data.hasOwnProperty(i)) {
        var element = json.data[i];
    }
}
share|improve this answer
    
Why the if (json.data.hasOwnProperty(i)) ? Your for statement already assign the right value in the i variable, so you don't have to perform check, it will always be true. –  DaFunix Jun 17 '13 at 16:35
    
Thank you for the answer. –  Jonh Smid Jun 17 '13 at 16:35
    
Using for ... in with arrays is generally a bad practice. There is no guarantee that you will get the elements in array order. Array.forEach however, would be a good choice. –  Jeremy J Starcher Jun 17 '13 at 16:39
1  
@DaFunix - hasOwnProperty is used to filter out anything that has been been monkey-patched into the Array.prototype. For instance, if there was a shim to add a forEach or indexOf method. –  Jeremy J Starcher Jun 17 '13 at 16:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.