I have the following Class structure
public class ResultData
{
[JsonProperty(PropertyName = "Id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "Results")]
public IEnumerable<Result> Results{ get; set; }
}
public class Result
{
public string Name { get; set; }
public string Value { get; set; }
}
Here is my code to convert to csv
private static void ToCsv(ResultData data)
{
StringBuilder csv = new StringBuilder();
var json = JsonConvert.SerializeObject(data);
var config = new ChoJSONRecordConfiguration {AllowComplexJSONPath = true};
config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("Id"));
config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("Results", "Name") { FieldType = typeof(string[]) });
config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("Results", "Value") { FieldType = typeof(string[]) });
using (var r = ChoJSONReader.LoadText(json, config))
{
using var w = new ChoCSVWriter(csv).WithFirstLineHeader().UseNestedKeyFormat(false);
w.Write(r);
}
File.WriteAllText("2.csv", csv.ToString());
}
Json
{"Id":"839c0a09-f2d0-4f29-9cce-bc022d3511b5","Results":[{"Name":"ABC","Value":"5"},{"Name":"CDE","Value":"2"}]}
What I would like is a CSV file With the Id and the results name in one column and the results value in another column