I am writing a method to write the content from a URL to disk as JSON. It should only write the response body content, and should ignore null values, i.e., if it encounters a null value it should not be written to the file. In order to just get the body content from the response I use the ReadAsStringAsync method to convert it to a string. I then create a new JObject from the string, and to remove the null values I use NullValueHandling.Ignore when serializing, however when I open the Json file there are still null values present. Below is my code:
var response = await client.GetAsync(url);
var data = await response.Content.ReadAsStringAsync();
JObject jsonObj = JObject.Parse(data);
var result = JsonConvert.SerializeObject(jsonObj, Formatting.Indented,
new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
I have tried solutions such as the one found here, but that leads to errors of its own.
Is there a way to be able to skip null values, while only saving the response body and not the headers?