1

I have a one Model it's look like

public class DataClass
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string ContactNo { get; set; }
}

and I tried to convert in Json request using below mentioned code.

var l=new List<Data>();
l.Add(new Data(){Name="foo",Address ="bar",ContactNo =123});
l.Add(new Data(){Name="biz",Address ="baz"});
string json=JsonConvert.SerializeObject(l);

it will give me string like

 [{"Name":"foo","Address":"bar","ContactNo":"123"},{"Name":"biz","Address":"baz","ContactNo":""}]

in output second ContactNo has a empty string but I don't need the field which has a no value or NULL . can anyone please tell me what is the best way to avoid NULL or Empty field from Json request? Thanks in Advance.

1
  • a) What is Data? b) ContactNo can not be empty(or "123") string since it is integer Commented Sep 1, 2013 at 16:56

3 Answers 3

1

Change your model as below

public class Data
{
    public string Name { get; set; }
    public string Address { get; set; }
    public int? ContactNo { get; set; }
}

and then serialize your object as below

var result = JsonConvert.SerializeObject(
             l, 
             new JsonSerializerSettings() 
             { 
                 NullValueHandling = NullValueHandling.Ignore 
             });
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your quick response but when I tried string messageToSend = JsonConvert.SerializeObject(api,converter, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); it will give me error like Argument 3: cannot convert from 'Newtonsoft.Json.JsonSerializerSettings' to 'Newtonsoft.Json.JsonConverter'
Your are passing three arguments please check my answer again
Ya but I need second argument to convert datetime so can you please tell me how it's possible?
it ignores null values only. How to handle empty values??
1

I assume you are using Json.Net.

You can use the System.ComponentModel.DefaultValueAttribute. This allows you to mark a property to use a different default value than null.

So, if you want empty strings to be ignored in your JSON output you can update the model class to look like this:

public class DataClass
{
    [DefaultValue("")]
    public string Name { get; set; }
    [DefaultValue("")]
    public string Address { get; set; }
    [DefaultValue("")]
    public string ContactNo { get; set; }
}

Note that the SerializerSettings.DefaultValueHandling must be set to Ignore or IgnoreAndPopulate for this to be picked up.

A more thorough example of various approaches for reducing serialized json size is here:

http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size

2 Comments

Note: I am a little uncertain if you intended ContactNo to be an int instead of a string. The accepted answer is an int, but the question is edited. I assume this is a relevant answer since the question specifically asks for the empty string.
I know ;) But I originally arrived here from google, looking for the answer. Since others with the same question could end up here I went back to add the answer after I had solved my problem.
0

1.You may add a flag in the model class.

public class DataClass{
public bool isIllegal{get;set;}
public string Name { get; set; }
public string Address { get; set; }
public string ContactNo { get; set{isIllegal=!string.isNullOrEmpty(value);)}
}

2.You can filter data whose isIllegal is false after JsonConvert.SerializeObject(l).

1 Comment

you mean I have to check each and every property with isIllegal? that is not a proper solution what if I have 25 property in model.

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.