question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullValueHandling.Ignore doesn't ignore nulls

See original GitHub issue

Source/destination types

System.Collections.Generic.Dictionary<string, object>

Source/destination JSON

{"exists":1,"doesnt":null}

Expected behavior

That the null is not included.

Actual behavior

The null is included.

Steps to reproduce

using System.Collections.Generic;
using Newtonsoft.Json;

namespace Sample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var obj = new Dictionary<string, object>
            {
                ["exists"] = 1,
                ["doesnt"] = null,
            };

            var json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            System.Console.WriteLine(json);
        }
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Kamareycommented, Dec 10, 2019

Same for me.

This example from documentation doesn’t work: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm

I tried it here: https://dotnetfiddle.net/5683os

Would be helpful to know when this feature stopped to work. I mean what the last version it worked

0reactions
JamesNKcommented, Feb 23, 2021

This setting is only for properties on objects. Not dictionaries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON.NET NullValueHandling in JsonPropertyAttribute not ...
Try passing new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } as third parameter in JsonConvert.
Read more >
Newtonsoft.Json NullValueHandling.Ignor doesn't ...
I have this class with one optional field which I want to hide from JSON when it's null, but it's there even with...
Read more >
Newtonsoft Json.Net serialize JObject doesn't ignore nulls ...
The thing is that when adding the NullValueHandling property to NullValueHandling.Ignore , every null property gets ignored, but only if it's part of...
Read more >
NullValueHandling setting
This sample serializes an object to JSON with NullValueHandling set to Ignore so that properties with a default value aren't included in the...
Read more >
How to ignore properties with System.Text.Json
In this article, you learn how to ignore properties based on various criteria: Individual properties; All read-only properties; All null-value ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found