Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Json object

Tags:

json

html

css

I have following JSON object

var s = {
    "TenantGroupName": "Fun Holidays",
    "queue_details": [{
        "queue_key": "1",
        "channelId": "1",
        "queue_name": "North India Travel",
        "contacts": [{
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "8:05",
            "service_level_breached": "true",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "8:34",
            "service_level_breached": "true",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "1",
        "queue_name": "South India Travel",
        "contacts": []
    }, {
        "queue_key": "1",
        "channelId": "1",
        "queue_name": "Summer 2012",
        "contacts": [{
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "[email protected]",
            "subject": "Redemption Coupon",
            "queue_time": "9:34",
            "service_level_breached": "true",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "3",
        "queue_name": "Honeymoon",
        "contacts": [{
            "from": "Henry Williams",
            "subject": "New Zealand",
            "queue_time": "1:45",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "Linda Simpson",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "7",
        "queue_name": "Feedback_SMS",
        "contacts": [{
            "from": "@sanjeev",
            "subject": "Maldives",
            "queue_time": "1:45",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "5",
        "queue_name": "Feedback_Twitter",
        "contacts": [{
            "from": "9833202859",
            "subject": "Wish to travel ...",
            "queue_time": "1:45",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }]
}

I want to loop through above JSON array. I have written the following in Javascript

I passed above JSON object into Javascript as follows

function CreateQueue(jString) {
    var jSonString = { "MyQueue": jString };
    alert(jSonString.MyQueue.length);
}

The above function alerts: undefined. How can I loop through above JSON string?

like image 833
ghanshyam.mirani Avatar asked Aug 31 '26 20:08

ghanshyam.mirani


2 Answers

this whole construct is an object, not an array, so you can't call .length on it. Also i dont understand why you wrap it into an additional object, which has absolutely no use.

To loop over all properties of an object, do the following:

for (var key in jString) {
   if (jString.hasOwnProperty(key) ){
       console.log( "key:"+key+", val:"+jString[key] );
   }
}

to loop over an array, do the following:

for (var i = 0; i < s.queue_details.length; i++){
  console.log( i+":"+s.queue_details[i] );
}
like image 181
Christoph Avatar answered Sep 02 '26 09:09

Christoph


The easiest way I find to work with json and XML parsing / iteration on the client-side is to include jQuery in your scripts.

I've created a small example for you that demonstrates looping through the first queue's contact entries and generates a formatted link output for each entry.

jsFiddle JSON / jQuery example.

Essentially the hard word is taken care of with
$.each(json, function (index, item) { /*...*/ });

like image 23
Brian Scott Avatar answered Sep 02 '26 11:09

Brian Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!