Unwind not working as expected
See original GitHub issueIt looks like Unwind is not working as expected in v 5.0.1 when using nested JSON data
I have even looked at the example code that is mentioned in the below issue where I see the same result. https://github.com/zemirco/json2csv/issues/414
json2csv: 5.0.1 node: 12.18.2
JSON Payload contents with the nested part.
createdAt: "2020-07-19T02:36:28.391Z" devicesensors: Array(1) 0: {sensorid: "temp", unit: "c", ultopic: "temp", dltopic: "temp"} id: "5f13b1ac338f7823c7dcc9ae" location: Array(1) 0: {lat: "100", lng: "100", gps: true, locatedat: "100", wifi: true} make: "testm" model: "model" vendor: "test"
Function using unwind
public convertToCSV(objArray: any, optfields?) {
const transforms = [unwind({ paths: ['location', 'devicesensors'] }), flatten({arrays:true})];
let json2csvParser = new this.Json2csvParser({fields: optfields, unwind: transforms});
let csv = json2csvParser.parse(objArray);
console.log(csv);
let file = new Blob([csv], { type: 'text/csv;charset=utf-8' });
saveAs(file,"data.csv");
return csv;
}
It results in the following output `“vendor”,“make”,“model”,“type”,“location.lat”,“location.lng”,“location.gps”,“location.locatedat”,“location.wifi”,“devicesensors.sensorid”,“devicesensors.unit”,“devicesensors.ultopic”,“devicesensors.dltopic”, “test”,“testm”,“model”,“sensor”,“2G”
Can you please let me know if I am doing something wrong or if this is a bug introduced in 5.0.1?
Thanks
Issue Analytics
- State:
- Created 3 years ago
- Comments:12
Can also be achieved with a custom
transform
(see the docs)Was requested in #442 but it hasn’t been implemented yet
If you want more help on how to achieve 1., I’d need an example JSON object and the CSV that you would like to get after parsing.
@juanjoDiaz
For the code I posted in #442, when I reconstruct the CSV back to JSON, for some reason there is an extra double quote for the last column in the row. Can you please help me look into it?
Other than that I think we can close this issue and use #442 to track the one I just mentioned.
Thanks in advance.