SamuraiJack
SamuraiJack

Reputation: 5539

How to loop through properties of a JSON object?

I am trying to loop through the properties of JSON object, but unable to do so.

var ds={
  "Table": [
    {
      "SrNo": 1,
      "AuctionName": "test auction",
      "AuctionDescription": "auction desc",
      "Testproject": "Y",
      "State": "India",
      "City": "2",
      "CompanyName": "IIFL",
      "Commodity": "10001",
      "BaseLineSpend": "50000",
      "ContractMonths": "5",
      "Owner": "arbaaz",
      "PreviewBids": "Y",
      "PrebidEndTime": "2015-09-11T18:00:00",
      "BiddingStartTime": "2015-09-10T18:00:00",
      "FirstTimeRunTime": 10,
      "TimeBtwLotClosing": 15,
      "BidRank": "20",
      "StartOverTime": 25,
      "OverTime": 30,
      "Buffer": "Y",
      "ImproveBidBy": "PERCENTAGE",
      "TieBids": "Y",
      "ActiveObservers": "Babitha G-C140492,",
      "Observers": "Tabrez Abdul Aziz Shaikh-A185615,",
      "ProjectOwner": "Tahir - Siddiqui-C107037,Tahir Ali-C132420,",
      "Administrator": "Rabi Roy-V182597,Gagan Kondalana Poonacha-C134452,Rabindra Kumar Choubey-C139454,",
      "GUID": "200869b0-e6be-4642-95ec-97509e457d63",
      "MkrId": "C123627",
      "MkrDt": "2015-09-03T16:23:15.917",
      "IsCreated": null
    }
  ]
}

Based on other similar question on stackoverflow. I tried:

 var DataSet =  jQuery.parseJSON(ds);                 
    var json_parsed = DataSet.Table;      

var items = json_parsed.Items;   // this is always undefined in console.
for (var i = 0; i < items.length; ++i) {
    console.log("Item #" + i);
    for (var name in items[i]) {
        alert(name + "=" + items[i][name]);
    }
}

I get undefined at json_parsed.Items; in console.

I am expecting property names to be displayed in alert eg: Srno, AuctionName .. so on.

Upvotes: 1

Views: 147

Answers (2)

Beroza Paul
Beroza Paul

Reputation: 2117

I see you have jQuery in there. The Javascript approach is good. But if you want to loop through a JSON object in jQuery, then you can try following code:

var ds={
  "Table": [
    {
      "SrNo": 1,
      "AuctionName": "test auction",
      "AuctionDescription": "auction desc",
      "Testproject": "Y",
      "State": "India",
      "City": "2",
      "CompanyName": "IIFL",
      "Commodity": "10001",
      "BaseLineSpend": "50000",
      "ContractMonths": "5",
      "Owner": "arbaaz",
      "PreviewBids": "Y",
      "PrebidEndTime": "2015-09-11T18:00:00",
      "BiddingStartTime": "2015-09-10T18:00:00",
      "FirstTimeRunTime": 10,
      "TimeBtwLotClosing": 15,
      "BidRank": "20",
      "StartOverTime": 25,
      "OverTime": 30,
      "Buffer": "Y",
      "ImproveBidBy": "PERCENTAGE",
      "TieBids": "Y",
      "ActiveObservers": "Babitha G-C140492,",
      "Observers": "Tabrez Abdul Aziz Shaikh-A185615,",
      "ProjectOwner": "Tahir - Siddiqui-C107037,Tahir Ali-C132420,",
      "Administrator": "Rabi Roy-V182597,Gagan Kondalana Poonacha-C134452,Rabindra Kumar Choubey-C139454,",
      "GUID": "200869b0-e6be-4642-95ec-97509e457d63",
      "MkrId": "C123627",
      "MkrDt": "2015-09-03T16:23:15.917",
      "IsCreated": null
    }
  ]
}


var jsonData = ds.Table[0];
$.each(jsonData, function(index, value) {
   console.log( index + '=' + value);
});

Upvotes: 0

Andy
Andy

Reputation: 63524

Well, Table is an array, for a start. Second, even if you used Table[0].Items, there's no Items property, which is why you're getting "undefined".

So try this instead:

var items = json_parsed[0];
for (var name in items) {
    alert(name + "=" + items[name]);
}

DEMO

Upvotes: 1

Related Questions