Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
How to format values using json2csv in javascript?

I'm using json2csv v5.0.6 for a small project and I wanted to format some values using custom formatters so I get a clean CSV file.

However, I can't seem to make the formatters work. I have one number formatter and one string formatter that are supposed to be called upon parsing. Here's a sample test file that reproduces this behaviour, with two simple formatters:

// test.js
const json2csv = require("json2csv")

const data = [{ name: "John", age: 20 }, { name: "Jessica", age: 32 }]

const customNumberFormatter = () => {
    return (value) => {
        return "0"
    }
}

const customStringFormatter = () => {
    return (value) => {
        return value[0]
    }
}

const csvOpts = {
    delimiter: "|",
    formatters: {
        number: customNumberFormatter,
        string: customStringFormatter,
    },
}

const parser = new json2csv.Parser(csvOpts)
const csv = parser.parse(data)
console.log(csv)

// console output
"name"|"age"
"John"|20
"Jessica"|32

// expected output
"name"|"age"
"J"|0
"J"|2

According to the documentation, the following should work, but it does not. So I'm probably doing something wrong but I have not been to figure out what.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to use the alpha version : json2csv@6.0.0-alpha.0

The last released version has some issue with formatters : https://github.com/zemirco/json2csv/issues/521 (they are not exported)

And you also have to call your formatters functions

const csvOpts = {
    delimiter: "|",
    formatters: {
        number: customNumberFormatter(),
        string: customStringFormatter(),
    },
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!