question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unwind not working as expected

See original GitHub issue

@juanjoDiaz

It 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:closed
  • Created 3 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
juanjoDiazcommented, Jul 26, 2020
  1. Can also be achieved with a custom transform (see the docs)

  2. 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.

0reactions
argupta23commented, Jul 30, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongodb $unwind aggregation not working - Stack Overflow
If you $unwind an array field like items.discounts that's empty, the resulting output is no documents. To preserve these documents, ...
Read more >
I don't get unwind in aggregation - why do I need it? - Studio 3T
The default answer is that no document is generated at all, because there's nothing to work with. And that's why there's ...
Read more >
Help re-grouping after unwind - Working with Data - MongoDB
I am implementing the Event Sourcing & CQRS patterns. I have a collection of documents with the events from users to manipulate models....
Read more >
Unwinding - The Rustonomicon
Like Erlang, Rust had lightweight tasks, and tasks were intended to kill ... Ideally, you should only panic for programming errors or extreme...
Read more >
Cannot unwind from first chance exceptions
The behavior you are seeing is expected, we do not unwind from first chance exceptions if Just My Code is disabled. There are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found