juanjoDiaz / json2csv

Flexible conversion between JSON and CSV
https://juanjodiaz.github.io/json2csv/
MIT License
306 stars 32 forks source link

How to specify flattened columns with argument "fields" #44

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:
```csv
"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.

lhp0730 commented 12 months ago

May I ask how it was done ?

tjvonbr commented 11 months ago

@tetsu9923 were you able to reach a solution? I'm running into the same issue recently.

juanjoDiaz commented 11 months ago

CSV is a format that requires a static number of columns. For that reason it doesn't make sense to have an array of unknown length. You might end up with tons of columns and with lots of blanks.

If you really want to do this, the only way is to preprocess the data to extract the list of possible headers and filter the ones that you want.