Skip to content

Nested JSONObject not correctly mapping to csv #109

@timisenco2015

Description

@timisenco2015

I am using your package to convert nested JSON objects to CSV but it is not correctly mapping my JSON Object data to CSV.
I have the sample JSON data, the CSV file, and my Node JS code below.
a) CSV file form_buggy_test_submissions.csv
b) JSON data [ { "form": { "confirmationId": "5AB5D586", "formName": "form_buggy_test", "version": 1, "createdAt": "2022-11-04T23:11:37.912Z", "fullName": "Ayobami Idowu", "username": "test", "email": "timiuser@gmail.com" }, "email": "timiuser@gmail.com", "submit": true, "oneRowPerLake": [ { "dataGrid": [ { "fishType": "rainbow", "numberKept": 1, "numberCaught": 1 }, { "fishType": "cutthroat", "numberKept": 1, "numberCaught": 1 }, { "fishType": "dollyVardon", "numberKept": 1, "numberCaught": 1 }, { "fishType": "smallmouthBass", "numberKept": 2, "numberCaught": 2 } ], "lakeName": "bron", "closestTown": "brondan", "numberOfDays": 1 }, { "dataGrid": [ { "fishType": "rainbow", "numberKept": 2, "numberCaught": 2 }, { "fishType": "cutthroat", "numberKept": 1, "numberCaught": 1 } ], "lakeName": "dran", "closestTown": "dranard", "numberOfDays": 1 } ], "fishermansName": "Ayobami", "didYouFishAnyBcLakesThisYear": "yes", "forWhichBcLakeRegionAreYouCompletingTheseQuestions": 8 } ]
c) JSON Header
[ 'form.confirmationId', 'form.formName', 'form.version', 'form.createdAt', 'form.fullName', 'form.username', 'form.email', 'fishermansName', 'email', 'forWhichBcLakeRegionAreYouCompletingTheseQuestions', 'didYouFishAnyBcLakesThisYear', 'oneRowPerLake.lakeName', 'oneRowPerLake.closestTown', 'oneRowPerLake.numberOfDays', 'oneRowPerLake.dataGrid.fishType', 'oneRowPerLake.dataGrid.numberCaught', 'oneRowPerLake.dataGrid.numberKept' ]
d) Node JS
`const options = {
fillGaps: false,
fillTopRow: true,
headerPathString:".",
verticalOutput:false,
// Leaving this option here if we need it. This is the original csv setup where the submission just prints out in 1 column as JSON
//definitions to type cast
typeHandlers: {
Object: function (value, name) {
if (value instanceof Object) {
if (name === 'submission') {
// not really sure what the best representation for JSON is inside a csv...
// this will produce a "" string with all the fields and values like ""field"":""value""
return JSON.stringify(value);
}
}
return value;
}
},

    // re-organize our headers to change column ordering or header labels, etc
    headers: await service._buildCsvHeaders(form, data)
  };
  const csv = await jsonexport(data, options);`

# Help Wanted
@andrew @dydx @cburgmer @jclay

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions