1

I am doing an C# Web Api Application using Framewrok 4.5

The method retrieve a class defined like this

public class BGBAResultadoOperacion
    {

        public string Codigo { get; set; }
        public string Severidad { get; set; }
        [DataMember(Name = "Descripcion", EmitDefaultValue = false)]
        public string Descripcion { get; set; }
    }

I need to NOT retrieve those Properties that are NULL. For that reason I defined Descripcion property like

[DataMember(Name = "Descripcion", EmitDefaultValue = false)]

As I can not remove a property from a class, I convert the class to Json

 var json = new JavaScriptSerializer().Serialize(response);

Where response is an instance of BGBAResultadoOperacion class.

But the Json generated say "Descripcion":"null"

I can not use Json.Net because I am using Framework.4.5.

How can I retrieve data avoiding properties that are null?

Thanks

5
  • 1
    "I can not use Json.Net because I am using Framework.4.5." - Json.NET goes down to .NET Framework 2.0; what makes you think you can't use it? Also: does DataContractJsonSerializer work here? that may do the job? (although it would be a poor cousin to Json.NET) Commented Feb 6, 2019 at 15:24
  • codereview.stackexchange.com/questions/147856/… Commented Feb 6, 2019 at 15:27
  • I traid to add Json.Net via Nuget and says it can not be added becouse i am using framework 4.5, I am not using DataContractJsonSerializer, i have to check out this. thanks Commented Feb 6, 2019 at 15:28
  • 1
    "Successfully installed 'Newtonsoft.Json 12.0.1' to ConsoleApp35" - works fine here; are you using an old IDE / tooling? Commented Feb 6, 2019 at 15:40
  • OK, i have installed Newtonsoft.Json 12.0.1, I have tried to install Json.Net, that was the error... After I installed it, can you tell me how to follow to avoid null properties? thanks!! Commented Feb 6, 2019 at 15:48

1 Answer 1

2

Use the NullValueHandling option when Serializing using Newtonsoft.Json.

From the documentation:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person Partner { get; set; }
    public decimal? Salary { get; set; }
}

Person person = new Person
{
    Name = "Nigal Newborn",
    Age = 1
};

string jsonIncludeNullValues = JsonConvert.SerializeObject(person, Formatting.Indented);

Console.WriteLine(jsonIncludeNullValues);
// {
//   "Name": "Nigal Newborn",
//   "Age": 1,
//   "Partner": null,
//   "Salary": null
// }

string jsonIgnoreNullValues = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(jsonIgnoreNullValues);
// {
//   "Name": "Nigal Newborn",
//   "Age": 1
// }
Sign up to request clarification or add additional context in comments.

Comments

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.