How can I convert a Json with nested properties into a DataTable
for export as a csv file in a generic manner using c#. A sample json as follows:
{
"Length": {
"Value": 12.93,
"Unit": "m"
},
"Temperature": {
"Value": 28.32,
"Unit": "DegC"
},
"Color": "Blue"
}
which I would like to flatten out in a table as follows:
"Property" | "Value" | "Unit"
---------------------------------
"Length" | 12.93 | "m"
"Temperature" | 28.32 | "DegC"
"Color" | "Blue" |
Note that the Color
property is not nested whereas the Length
and Temperature
properties are. In the case of a unit-less property such as Color
nothing (or null) should be added to the "Unit" column in the data table.
Additionally the nested properties would mostly be of type double
, but could contain any struct
type.
Convert JSON to DataTable 's solution is the closest I have come, but of course does not work for the nested properties I have.