Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to parse/Deserilize JSon response?

(0) ShareShare
ReportReport
Posted on by 30

I want to parse/Deserilize JSon respones in C#, the Json looks as the one below:

{
	"BusinessUnitId": "",
	"CorrelationId": "",
	"Depth": 1,
	"InitiatingUserAzureActiveDirectoryObjectId": "",
	"InitiatingUserId": "",
	"InputParameters": [
		{
			"key": "Target",
			"value": {
				"__type": "Entity:http://schemas.microsoft.com/xrm/2011/Contracts",
				"Attributes": [
					{
						"key": "firstname",
						"value": ""
					},
					{
						"key": "lastname",
						"value": ""
					},
...

how can I get the field value such as firstname, lastname..? when using this approach still can't find the value by key

public class Employee
{
    public string empNumber { get; set; }
    public Address homeAddress { get; set; }
    public IList aliasNames { get; set; }
}

public class Address
{
    public string line1 { get; set; }
    public string line2 { get; set; }
}

public class Names
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

   

Employee objOD = JsonConvert.DeserializeObject(respJSON);

  • q1938484 Profile Picture
    q1938484 30 on at
    RE: How to parse/Deserilize JSon response?

    Thanks, but how to access now firstname or last name from it?

    firstname = InputParameters[0].value.Attributes.Where(x => x....)

  • Suggested answer
    vishal bagadia_crm Profile Picture
    vishal bagadia_crm 490 on at
    RE: How to parse/Deserilize JSon response?

    Hello ,

    The response you are is a nested json. Thus to deserialize it, you would need a Nested class like structure.

    Based on your input json, I tried to create a structure. Please use the following class objects to deserialize your json response.

    // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); 
        public class Attribute
        {
            public string key { get; set; }
            public string value { get; set; }
        }
    
        public class Value
        {
            public string __type { get; set; }
            public List Attributes { get; set; }
        }
    
        public class InputParameter
        {
            public string key { get; set; }
            public Value value { get; set; }
        }
    
        public class Root
        {
            public string BusinessUnitId { get; set; }
            public string CorrelationId { get; set; }
            public int Depth { get; set; }
            public string InitiatingUserAzureActiveDirectoryObjectId { get; set; }
            public string InitiatingUserId { get; set; }
            public List InputParameters { get; set; }
        }
    
    


    Please mark the answer as verified, if it solves your concern.

    Regards,
    CRM Developer

  • q1938484 Profile Picture
    q1938484 30 on at
    RE: How to parse/Deserilize JSon response?

    I would like to read all the attributes from my JSon:

    ..

    "Attributes": [

    {

    "key": "firstname",

    "value": ""

    },

    ..

    I have been trying to use my model class but I don't know how to get the values

    Employee objOD = JsonConvert.DeserializeObject<Employee>(respJSON);

  • Nya Profile Picture
    Nya 29,058 on at
    RE: How to parse/Deserilize JSon response?

    Hi,

    Would you please provide a more detailed description of the requirements your code is trying to achieve?

    Best Regards,

    Nya

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,476 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans