I'm using JSON.NET in c# to reach out to an API that provides JSON as the output.
I print that output to the console as CSV so that it can be consumed by powershell convertfrom-csv, format-table, select-object and so on.
I've got a decent generic method for printing all kinds of JSON as CSV and it works almost all the time.
But, in some cases, I get JSON like this
{
"result":
[
{ "product": "SP1", "version": "6.0" },
{ "product": "SP1 hf 40", "version": "6.0.40" },
{ "product": "SP1 hf 50", "version": "6.0.50" },
{ "version": "6.0.100" }
]
}
Where the last item from result is missing the product property entirely.
Is there a way to iterate over the items in result and put in the missing properties such that the output is:
{
"result":
[
{ "product": "SP1", "version": "6.0" },
{ "product": "SP1 hf 40", "version": "6.0.40" },
{ "product": "SP1 hf 50", "version": "6.0.50" },
{ "product": "", "version": "6.0.100" }
]
}
This will allow me to continue using the generic method for printing the JSON output as a CSV.
One idea I had would be to iterate through the result array and find the element that had the most properties and use that as the basis for printing the CSV.
But that idea falls pretty flat on its face because the number of properties doesn't necessarily guarantee all the properties across the entire set.
I think I almost need a UNION type operation that will find all the properties in all the elements of an array.
Sorry if this is a really dumb question but I was just wondering if it was possible.