zemirco / json2csv

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

How to specify flattened columns with argument "fields"? #584

Closed tetsu9923 closed 1 year ago

tetsu9923 commented 1 year ago

Thank you for providing great JSON to CSV converter. We are getting trouble with specifying columns that are flattened by transforms: [flatten({ arrays: true })]. Details are shown below.

Filing an issue

  1. Include the version of json2csv used. 7.0.1

  2. Include your node version/browser vendor and version. 18.9.1

  3. Include the command or code you used.

    
    import { Parser } from "@json2csv/plainjs"
    import { flatten } from "@json2csv/transforms"

const data = [ { car: "Audi", price: 40000, color: ["black", "blue"], # The length of the array is unknown until it is processed. }, { car: "BMW", price: 35000, color: ["red", "blue"], # The length of the array is unknown until it is processed. }, ]

const fields: Array = ["car", "color"]

const parser = new Parser({ fields: fields, transforms: [flatten({ arrays: true })] }) const csv = parser.parse(data) console.log(csv)


4. Include a sample dataset that we can test against.
None (Included in the code above)

5. Include your output/error.

Expected output:

"car","color.0","color.1" "Audi","black","blue" "BMW","red","blue"


Current result:

"car","color" "Audi", "BMW",



The length of the array is unknown until it is processed, so we cannot set fields argument like `["car", "color.0", "color.1"]`.
We are looking for the solution.
Thank you.
knownasilya commented 1 year ago

This repo moved to https://github.com/juanjoDiaz/json2csv