asafdav / ng-csv

Simple directive that turns arrays and objects into downloadable CSV files
MIT License
573 stars 215 forks source link

Handling nested objects #126

Closed raghuugare closed 7 years ago

raghuugare commented 8 years ago

Hi,

I am trying to fetch some records from MongoDB, where there are some properties whose value itself is a nested JSON object. Is there an easy method to 'un-pack' the nested properties into top-level CSV columns.

E.g., suppose I have an array of n objects of the following format: { first_name: 'Raghu', last_name: 'Ugare', location: { 'city': 'Cambridge', 'state': 'MA' } }

I know we can easily map first_name & last_name as columns. But is there a simple way to map location.city & location.state as well? Or should we do it manually?

Thanks for this anyways!

shaohaolin commented 8 years ago

@raghuugare The reason why handling nested objects is not implemented is there are so many different cases in nested json objects. It can be nested objects as the example you gave or it can be nested objects with nested arrays. I faced the same problem as well; however, it is doable depends on individual case.

First, you need to consider the format of the CSV. Using your example and I want my CSV looks like: result

And then convert your example to an array of json objects (get rid of all the nested objects or arrays into a single array of objects): [{first_name: 'Raghu', last_name: 'Ugare', 'city': 'Cambridge', 'state': 'MA' } ]

I will suggest you to take a look underscore.js library to help you manipulating objects.

adyba commented 8 years ago

The directive like this should not be responsible for JSON manipulation, there is plenty of libraries you can use to resolve your cyclic references, nested objects etc. JSON3, JSON-js, JSONPath. Sometime in a future galaxies away someone could send the PR with unparse part of the https://github.com/mholt/PapaParse

trantrunghieu0809 commented 8 years ago

hi @raghuugare , I have a small project, it used to export excel from json. I support nested json.You can refer

https://github.com/dtagdev1/json-export-excel

http://plnkr.co/edit/6ieuJ1khmKFds9VYHoDv

raghuugare commented 7 years ago

Thanks a lot @shaohaolin , @adyba and @dtagdev1 !

ForkInSpace commented 5 years ago

@raghuugare The reason why handling nested objects is not implemented is there are so many different cases in nested json objects. It can be nested objects as the example you gave or it can be nested objects with nested arrays. I faced the same problem as well; however, it is doable depends on individual case.

First, you need to consider the format of the CSV. Using your example and I want my CSV looks like: result

And then convert your example to an array of json objects (get rid of all the nested objects or arrays into a single array of objects): [{first_name: 'Raghu', last_name: 'Ugare', 'city': 'Cambridge', 'state': 'MA' } ]

I will suggest you to take a look underscore.js library to help you manipulating objects.

Imo the easiest solution. Thanks @shaohaolin