alexcaza / export-to-csv

Export a JS collection to CSV; written in TypeScript.
Other
207 stars 48 forks source link

How to parse nested JSON properties? #75

Closed sdancer75 closed 8 months ago

sdancer75 commented 8 months ago

Is there any way to parse nested JSON properties ? Currently, it shows them as [OBJECT]

         {
            name: 'Rouky',
            date: '2023-09-01',
            percentage: 0.4,
            quoted: '"Pickles"',
            Service: {
               active: 'yes',
               limit: 1,
            },
         },
alexcaza commented 8 months ago

Could you elaborate on what output you'd like from this input? How do you see nested properties being rendered in a CSV file?

The CSV spec doesn't support nested columns, which I'll not support in this library since the output is too ambiguous.

However, If you want something like the below as output:

name, Service, quoted, ...otherColumns
Rouky, active: yes | limit: 1, "Pickles", ...otherValues

You could transform the data before passing it to the generator function with something like:

let data = {
  name: 'Rouky',
  date: '2023-09-01',
  percentage: 0.4,
  quoted: '"Pickles"',
  Service: {
    active: 'yes',
    limit: 1,
  },
}
data.Service = `active: ${data.Service.active} | limit: ${data.Service.limit}`;
generateCsv(csvConfig)(data);
alexcaza commented 8 months ago

I'm going to close this issue for now. Feel free to open a discussion under ideas on how to handle nested properties if you feel strongly about this feature being something the library should handle.

I'm not keen on having the library apply transformations on arbitrary/unknown inputs since it's pretty easy to transform the data before you pass it in. But if someone has a suggestion for a clean API or obvious use-cases, I'm all ears 😄