I have a JSON file something like this:
{
"id": 2,
"name": "I.1.A.2",
"activeFlag": true,
"recipients": [
{
"id": 3,
"identityName": "idenity1",
"fullName": "FullName1"
},
{
"id": 4,
"identityName": "identity2",
"fullName": "FullName2"
}
]
}
I need to convert it to a CSV output similar to this using C# and dotnet Core.
"id","name","activeFlag","identityName"
"2","I.1.A.2","true","identity1;identity2"
However, I can only get the CSV output as:
"id","name","activeFlag","recipients_0", "recipients_1"
"2","I.1.A.2","true","identity1","identity2"
Here's the code that's generating the above output:
using (var csv = new ChoCSVWriter(".\\temp\\csvoutput.csv").WithFirstLineHeader()
)
{
using (var json = new ChoJSONReader(".\\temp\\tmpjson.json")
.Configure(c => c.ConvertToFlattenObject(arrayIndexSeparator: ';'))
.Configure(c => c.ArrayValueSeparator = ';')
.Configure(c => c.ArrayValueSeparator = ';')
.WithField("id", jsonPath: "$..id", isArray: false)
.WithField("recipients", jsonPath: "$..recipients[*]..identityName", isArray: true, fieldName: "recipients")
)
{
csv.Write(json);
}
}
Right now, I'm using the ChoEtl library, but open to other options/suggestions. Been searching for an answer to this issue and haven't found any yet. Sorry if there's some solution I haven't found yet. I did try a similar solution here: How to output JSON array as a single field in CSV using ChoETL But didn't quite get it to fit my needs.