zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 365 forks source link

Add meta support? #197

Closed pocesar closed 7 years ago

pocesar commented 7 years ago

I'm trying to dyamically export a CSV to an S3 depending on the columns the user picks. I have a few nested objects, and arrays, and I'm having a hard time to use it with flatten. basically I'm doing this:

const common = (name: keyof Data.CitizenDataSetMixin) => (d: Data.CitizenDataSet, field: any, meh: any) => {
  let value = _.get(d, name, '')
  switch (name) {
    /* do some value transforms here */
  }  
  return value/**/
}

  const csv = await bluebird.fromCallback<string>((resolve) => {
    json2csv<Data.CitizenDataSetMixin>({
      data,
      fields: selected.map((field: keyof Data.CitizenDataSetMixin) => {
        return {
          label: Data.CitizenFieldGuard[field] || field,
          value: common(field)
        }
      }),
      ...defaultCSVOptions
    }, resolve)
  })

the problem is that I have some paths that turns out as phone.0.value or documents.1.type, and since it's dynamic (some rows doesn't have all the information), I'm having a hard time 'translating' the paths before hand. I really don't want to transverse all the items and collecting their max number for each dynamic column to find out how to set the name (like transform document field chosen by the user to document.0.type, document.1.type, and so on). so I was thinking to passing some extra meta data, but having no idea how to even make a PR to implement this

pocesar commented 7 years ago

no other way around, I couldn't know how many would be, so I'm having to count them before hand