Closed Tobias-Keller closed 2 years ago
I have some touble to convert my json, what i am doing wrong?
JSON that i want to convert into csv:
[ { "email": "test@test.com", "ip": "xxx.xxx.xxx.xx", "created": "Sat Sep 11 2021 11:44:58 GMT+0200 (Mitteleuropäische Sommerzeit)", "results": [ {"title": "some title", "selected": true, "image": "someImagePath"}, {"title": "some other title", "selected": false, "image": "someImagePath"}, ] } ]
my code:
const fields = ['email', 'created', 'ip', 'results.title', 'results.selected']; const transforms = [unwind({paths: ['results'], blankOut: false}), flatten()]; const json2csvParser = new Parser({ fields: fields, transforms: transforms, delimiter: ';' }); csv = json2csvParser.parse(results);
result:
"email";"created";"ip";"results.title";"results.selected" ;;;"some title";true ;;;"some other title";false
expected result:
"email";"created";"ip";"results.title";"results.selected" test@test.com;Sat Sep 11 2021 11:44:58 GMT+0200 (Mitteleuropäische Sommerzeit);xxx.xxx.xxx.xx;"some title";true test@test.com;Sat Sep 11 2021 11:44:58 GMT+0200 (Mitteleuropäische Sommerzeit);xxx.xxx.xxx.xx;"some other title";false
without the transform, i getting the email, created, ip fields, but with not.
after reading some other issues, found that it not works correctly with the mongo db json. so stringify and parsing the json works: csv = json2csvParser.parse(JSON.parse(JSON.stringify(results)));
csv = json2csvParser.parse(JSON.parse(JSON.stringify(results)));
I have some touble to convert my json, what i am doing wrong?
JSON that i want to convert into csv:
my code:
result:
expected result:
without the transform, i getting the email, created, ip fields, but with not.