I have a JObject that is used as a template to call RESTful web services. This JObject is created using the analyzer and, since it is used as a template to tell the user what the endpoint circuit looks like, I needed to figure out a way to save all the properties, so I don't execute my values ββto null . As in the example, this is what the object originally looks like:
{ "Foo":{ "P1":null, "P2":null, "P3":null, "P4":{ "P1":null, "P2":null, "P3":null, }, "FooArray":[ { "F1":null, "F2":null, "F3":null, } ] }, "Bar":null }
The user can then fill in individual fields as needed, for example Foo.P2 and Foo.P4.P1 :
{ "Foo":{ "P1":null, "P2":"hello world", "P3":null, "P4":{ "P1":1, "P2":null, "P3":null, }, "FooArray":[ { "F1":null, "F2":null, "F3":null, } ] }, "Bar":null }
means that they care about all these two areas. Now I want to serialize this template ( JObject ) back to the JSON string, but I want only the fields that were filled to be displayed. So I tried this:
string json = JsonConvert.SerializeObject(template, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Unfortunately, this did not work. I came across this question and realized that the null value in the object is the actual JToken type and not really null , which makes sense. However, in this particular case, I need to get rid of these "unused" fields. I tried manually sorting the nodes and deleting them, but that didn't work either. Note that the only managed type that I use is JObject ; I do not have a model for transforming an object or defining attributes, since this "pattern" is allowed at runtime. I was just wondering if anyone ran into such a problem and she has any ideas. Any help is much appreciated!