How do I handle semicolons when creating a csv file?

I am using the npm json-csv module to create a csv file from an array of objects. However, some fields may contain a semicolon ( ;

), and apparently the csv gets semicolon delimited, even though this field is quoted. Any suggestions on how to fix this problem?

The code I am using for the parameters is the following:

var options = {
  fields: [
    {
      name : 'paragraphId',
      label : 'ParagraphID'
    },
    {
      name : 'paragraph',
      label : 'Paragraph',
      quoted : true
    }
  ]
};

      

Thank,

Eva

+3


source to share


1 answer


According to the CSV spec, you can have delimiters in values ​​if you surround those values ​​with double quotes. From the CSV spec :

Fields with embedded commas must be separated by double quotation marks.

and

Fields can always be separated by double quotes. Separators will always be discarded.



The ability to trigger this behavior when exporting data using a library json-csv

quoted: true

in the options for a given field - I can see that you have already enabled it, so you are good.

Also - it's worth noting that this library uses comma ( ,

) as the default separator, not semicolon ( ;

). To use a different delimiter, change the parameters correctly:

var options = {
  fields: [
    {
      name: 'paragraphId',
      label: 'ParagraphID'
    },
    {
      name: 'paragraph',
      label: 'Paragraph',
      quoted: true
  }],
  fieldSeparator: ';' // the important part!
};

      

+2


source







All Articles