I'm extracting Survey Responses from our survey provider via an API. I'm not using classes for this to de-serialize, it's just key / value pairs.
I'm reading an array of Responses from a file. Each Response contains many 'Response Items' we'll call them, as below:
[
{
"response_id":"1234",
"hRz5aMmGPf": null,
"6UnnAZSEBT": null,
"nGS1cyvLwK": "Red"
},
{
"response_id":"1235",
"hRz5aMmGPf": "John Smith",
"6UnnAZSEBT": null,
"nGS1cyvLwK": "Blue"
},
{
"response_id":"1236",
"hRz5aMmGPf": "Jane Doe",
"6UnnAZSEBT": "Yes",
"nGS1cyvLwK": null
}
]
For the purpose of this exercise, I'm reading the JSON from file as follows:
List<JToken> responseobjs = new List<JToken>();
JObject o = JObject.Parse(fcontents);
responseobjs.AddRange(o["results"].Children())
As per the sample data, there's a lot nulls in the source data which I'd like to eliminate in the quickest way possible. I've read about 'NullValueHandling' but this seems to only be applicable if I de-serialize into a Class / Object, which isn't possible with the distinct field ID's being returned by the feed.
Is it possible for me to take the Children() above but skip the nulls?
Right now I'm iterating over the Responses and then Response Items to remove nulls and it's taking a long time to transform.
foreach (JToken obj in responseobjs)
{
foreach (JProperty rprop in obj.Where(x=> x.HasValues==true).ToList())
{
if (rprop.Value.ToString().Trim() == "")
continue;
..Continue parsing here...
}
}