-1

I have the following JSON in which some attributes have empty string i.e. "" or null values

{  
   "allOrNone":false,
   "records":[  
  {  
     "Address__c":"Street",
     "ConsentToComm__c":"",
     "EmailCLDate__c":"",
     "attributes":{  
        "type":"Stage_FF_Hot_Alerts__c"
     }
  }
 ]
}

I have to remove the empty string and null value attributes from this JSON. How can i remove them. I am doing this in C#. Required JSON after removing empty strings and null would be:

{  
   "allOrNone":false,
   "records":[  
  {  
     "Address__c":"Street",
     "attributes":{  
        "type":"Stage_FF_Hot_Alerts__c"
     }
  }
 ]
}
10
  • to be clear: in this scenario, you just have JSON that you want to post-process - it isn't tied to a specific object model that you're serializing? (both ways are possible, but have very different implementations) Commented Jan 15, 2019 at 11:31
  • Yes, i want to process the JSON. I read data from database and store it in the DataTable, this data table is then serialized. Commented Jan 15, 2019 at 11:33
  • 3
    So, what's stopping you? Commented Jan 15, 2019 at 11:33
  • how can i remove the attributes with empty string values i.e. "". I dont want those attributes in my JSON. For more clarification you can see the input JSON and the JSON which i am expecting as output. Commented Jan 15, 2019 at 11:36
  • @Izzy since this isn't a serialization scenario, I don't think that applies Commented Jan 15, 2019 at 11:38

2 Answers 2

7

I have resolved this problem. I have removed the null values during serialization.

string JSONstring = JsonConvert.SerializeObject(dt, new 
JsonSerializerSettings()
{
            NullValueHandling = NullValueHandling.Ignore,
});

And after that empty string values are removed through the following code

var temp = JArray.Parse(JSONstring);
temp.Descendants()
    .OfType<JProperty>()
    .Where(attr => attr.Value.ToString() == "")
    .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
    .ForEach(attr => attr.Remove()); // removing unwanted attributes

JSONstring = temp.ToString();
Sign up to request clarification or add additional context in comments.

3 Comments

Yes @er-mfahhgk
then mark the tick on left side of answer to make it green otherwise people may effort to provide you solution.
Its my own answer, i can mark it after 2 days @er-mfahhgk
0

This may Help

namespace JSON
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Root
    {
        [DefaultValue("")]
        [JsonProperty("allOrNone")]
        public bool AllOrNone { get; set; }

        [DefaultValue("")]
        [JsonProperty("records")]
        public Record[] Records { get; set; }
    }

    public partial class Record
    {
        [DefaultValue("")]
        [JsonProperty("Address__c")]
        public string AddressC { get; set; }

        [DefaultValue("")]
        [JsonProperty("ConsentToComm__c")]
        public string ConsentToCommC { get; set; }

        [DefaultValue("")]
        [JsonProperty("EmailCLDate__c")]
        public string EmailClDateC { get; set; }

        [DefaultValue("")]
        [JsonProperty("attributes")]
        public Attributes Attributes { get; set; }
    }

    public partial class Attributes
    {
        [DefaultValue("")]
        [JsonProperty("type")]
        public string Type { get; set; }
    }

    public partial class Root
    {
        public static Root FromJson(string json) => JsonConvert.DeserializeObject<Root>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Root self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            ContractResolver = ShouldSerializeContractResolver.Instance,
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

1 Comment

I can't make any new class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.