web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JSON CONVERT Ignore Empty Object

(0) ShareShare
ReportReport
Posted on by 5

Hello community , 

I try to ignore an empty object on my Json  

this is my Json :

5327.json.png

{
"custNmae": "test n test p",
"firstName": "test n",
"lastName": "test p",
"updateDate": "2018-12-28T16:25:25Z",
"tutor": {}
}

Does any one have a clue ? 

Please let me know !!!!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: JSON CONVERT Ignore Empty Object

    Not sure you may have seen below thread .

    stackoverflow.com/.../newtonsoft-json-customcontractresolver-exclude-empty-object-nodes

  • LAMYAE Profile Picture
    30 on at
    RE: JSON CONVERT Ignore Empty Object

    Thanks for the reply Yeah I have already test it it doesnt work fo me 

  • Jamil Lamyae Profile Picture
    5 on at
    RE: JSON CONVERT Ignore Empty Object

    Any help please

  • Verified answer
    Jamil Lamyae Profile Picture
    5 on at
    RE: JSON CONVERT Ignore Empty Object

    I found a solution But I don't know if it's the best

    To summer up I have :

     ObjectA{

    id:"2323";

    Name:"testName";

    objectB{};

    }

    I modify in the contractresolver 

    if (property.PropertyType == typeof(ObjectB))
    {
    property.ShouldSerialize = instance =>
    {

    ObjectA e = (ObjectA)instance;
    if (e.objectB.Attribute1== null  && e.objectB.Attribute2== null ) //I need to check all the attributes
    {
    property.Ignored = true;
    return false;
    }
    else
    {
    return true;
    }
    };
    }

    Then call it in my jsonconvert :

    string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(customerB2C, Formatting.Indented, new JsonSerializerSettings
    {

    ContractResolver = new SkipEmptyContractResolver(),
    DefaultValueHandling = DefaultValueHandling.Ignore,
    NullValueHandling = NullValueHandling.Ignore
    });

     

    It's not the better one but at least it solve my issue 

    I don't know if any one have a better idea ?? 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans