Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Deserialize JSON to entity?

Posted on by Microsoft Employee

Using a webhook, I am able to setup a post image call to do a POST of the entity when the entity in CRM gets updated.

I see the JSON but now on my API, I want to deserialize the JSON to a concrete object of that type - the Entity type.

On the Web API I have, when using Newtonsoft's JSON to deserialize, it does not successfully deserialize it (no errors).

But I can get the JObject, which is not what I want.

How can I do this?

Or if possible, how can I get the entity type (in this case, contact) from the CRM Service by getting it by id so I can get all the data for that contact?

*This post is locked for comments

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Deserialize JSON to entity?

    I think you need to generate your own classes for the CRM entities. I've just remembered, the name of the fields returned by the WEB API is different than what you get when calling the SOAP endpoint (Example: SOAL customerid vs WEB API _customerid_value) so the CrmSvcUtil i think it won't help in this case.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Deserialize JSON to entity?

    So trying what you suggested, I still get an error about an unexpected carriage return (\r)

    Here is the code I am using to deserialize to the entity:

    public static Models.Contact DeserializeJsonString(string jsonString)
            {
                //create an instance of generic type object
                Models.Contact obj = new Models.Contact();
    
                var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
                obj = (Models.Contact)serializer.ReadObject(ms);
                ms.Close();
                return obj;
            }


    At the line:

    obj = (Models.contact)serializer.ReadObject(ms);

    I get this error:

    System.Runtime.Serialization.SerializationException: 'There was an error deserializing the object of type PoC.Dynamics.Sync.Models.Contact. Encountered invalid character ''.'

    And the json is sent from the webhooks in CRM. Post Image.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Deserialize JSON to entity?

    Generating the classes is around 20MB, which makes sense but how best to reduce this?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Deserialize JSON to entity?

    I will try this, but what about custom entities or custom attributes for an entity like contact?

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Deserialize JSON to entity?

    Have you tried to use the CrmSvcUtil to generate early bound classes for your CRM entities? Does it work afterwards to deserialize the object to the given entity type?

    See here details on how to use the svcutil tool: docs.microsoft.com/.../create-early-bound-entity-classes-code-generation-tool

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans