Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How do I parse OData response?

Posted on by 12,070 Super User 2024 Season 1

Hi all

I have this data response:

"{
"d" : {
"results": [
{
"__metadata": {
"uri": "http://win-bd86268lvbk/CRMDEVELOPMENT/XRMServices/2011/OrganizationData.svc/BusinessUnitSet(guid'69e04798-1576-e611-80e4-308d99276d29')", "type": "Microsoft.Crm.Sdk.Data.Services.BusinessUnit"
}, "Address1_StateOrProvince": "Sindh", "Address1_Line1": "Gulshan e Iqbal", "Address1_City": "Karachi", "BusinessUnitId": "69e04798-15768d99276d29", "Address1_PostalCode": "75300", "Address1_Country": "Pakistan"
}
]
}
}"

How do parse it using JavaScript.?

 I tried these options but fail to get values:

var retrieved = JSON.parse(req.responseText).d,
retrievedRecord = retrieved.results.Address1_Line1,
City = retrieved.results[0].Address1_City,
Country = retrieved.results[1].Address1_Country;

Only the highlighted one is working which gives me the above response.

Thank You

*This post is locked for comments

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How do I parse OData response?

    var City = retrieved.results[0].Address1_City;

    This syntax should work.

  • Verified answer
    jlattimer Profile Picture
    jlattimer 24,558 on at
    RE: How do I parse OData response?

    If you've queried multiple records, it would look something like this:

    var returned = JSON.parse(this.responseText).d;
    var results = returned.results;
    for (var i = 0; i < results.length; i++) {
        var address1_City = results[i].Address1_City;
        var address1_Line1 = results[i].Address1_Line1;
         var name = results[i].Name;
    }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans