juanjoDiaz/json2csv

How to specify flattened columns with argument "fields"

tetsu9923 opened this issue · 3 comments

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<string> = ["car", "color"]

const parser = new Parser({ fields: fields, transforms: [flatten({ arrays: true })] })
const csv = parser.parse(data)
console.log(csv)
  1. Include a sample dataset that we can test against.
    None (Included in the code above)

  2. 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.

May I ask how it was done ?

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

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.