-1

I've json response like this, I want to get all project names from this.

I have done as usual as iterate through arrays but here "d" is not an array. How can I do to get result:

{
    "d": {
    "results": [
        {
            "ProjectId": "696fcc7c-f355-e511-93f0-00155d008500",
            "ProjectName": "Payroll",
            "EnterpriseProjectTypeDescription": null,
            "EnterpriseProjectTypeId": null,
            "EnterpriseProjectTypeIsDefault": null
        },
        {
            "ProjectId": "696fcc7c-f355-e511-93f0-00155d008505",
            "ProjectName": "Permanant",
            "EnterpriseProjectTypeDescription": null,
            "EnterpriseProjectTypeId": null,
            "EnterpriseProjectTypeIsDefault": null
        }
    ]
    }
}
Anju
  • 631
  • 2
  • 9
  • 25

2 Answers2

3

Use Array.prototype.map():

var names = myData.d.results.map(function(item){
    return item.ProjectName;
});

This will result in an array like:

["Payroll", "Permanant"]

(Assuming myData is the object in your question)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • What's with the downvote? What's wrong with this answer? – Cerbrus Sep 29 '15 at 06:18
  • I didn't downvote you, but I'm assuming it's due to `Array.prototype.map` first being introduced in IE9. A lot of business applications are reliant on a minimum of IE8 (which even Google will drop support for in a month.. then again, the MDN link has a polyfill, so it shouldn't be an issue). – h2ooooooo Oct 12 '15 at 09:57
  • If that is the reason, then I feel sorry for the person that has to support IE8 ;-) – Cerbrus Oct 12 '15 at 10:03
  • Tell me about it! Hopefully Microsoft's "update everything to Windows 10" will get rid of the XP installations that can't upgrade to IE9. To think that there are still businesses relying on a 14 year old OS to function is beyond me! – h2ooooooo Oct 12 '15 at 10:05
1

You can use jsonObject.d.results to get the array from response and use forEach() to iterate array

var res = {
  "d": {
    "results": [{
        "ProjectId": "696fcc7c-f355-e511-93f0-00155d008500",
        "ProjectName": "Payroll",
        "EnterpriseProjectTypeDescription": null,
        "EnterpriseProjectTypeId": null,
        "EnterpriseProjectTypeIsDefault": null
      }, {
        "ProjectId": "696fcc7c-f355-e511-93f0-00155d008505",
        "ProjectName": "Permanant",
        "EnterpriseProjectTypeDescription": null,
        "EnterpriseProjectTypeId": null,
        "EnterpriseProjectTypeIsDefault": null
      }
    ]
  }
};

res.d.results.forEach(function(v) {
  document.write(v.ProjectName + '<br>')
})

If you want to get it as an array then you can use map()

var res = {
  "d": {
    "results": [{
      "ProjectId": "696fcc7c-f355-e511-93f0-00155d008500",
      "ProjectName": "Payroll",
      "EnterpriseProjectTypeDescription": null,
      "EnterpriseProjectTypeId": null,
      "EnterpriseProjectTypeIsDefault": null
    }, {
      "ProjectId": "696fcc7c-f355-e511-93f0-00155d008505",
      "ProjectName": "Permanant",
      "EnterpriseProjectTypeDescription": null,
      "EnterpriseProjectTypeId": null,
      "EnterpriseProjectTypeIsDefault": null
    }]
  }
};

var result = res.d.results.map(function(v) {
  return v.ProjectName;
})

document.write(JSON.stringify(result));
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188