Home / Apps / What is a CSV (comma separated value) file?

A CSV file is a simple text file that can be opened in a variety of programs. A CSV file (comma separated values) is a plain text file in which information divided by commas.

CSV files are most often found in spreadsheets and databases.

We can use a CSV file to move data between programs that are usually unable to exchange data.

What do we need to know about a CSV file?

A CSV file, as the name implies, usually shares information using commas. It is a way of exchanging structured information, such as the contents of a spreadsheet, between programs that may not necessarily read each other directly.

As long as two programs can open a CSV file, they can exchange data. For example, you can save contact information from Microsoft Excel as a CSV file and import it into the address book in Microsoft Outlook.

A CSV file looks like this, where each row contains the same data sequence, so every program that needs to read it knows what to expect:

• Product, Size, Color, Price
• Shirt, medium, blue, $ 14
• Shirt, large, red, $ 15
• Trousers, medium, blue, $ 23

Regardless of the name, a CSV file does not need to rely on commas as a separator between pieces of information. This separator, called the delimiter, can be a semicolon, space, or some other character, although commas are more common.

How to export a CSV file with NodeJS?

We will extract a CSV file using a Node.js module called json2csv. We will be using json2csv because it is an easy-to-use library – it can just take a set of objects and convert them to a CSV file.

Step 1:

In the terminal/command line type the npm command of json2csv in the directory where you want to install this module. If the module was successfully installed it should look like this:

+ json2csv@5.0.6

added 4 packages from 6 contributors and audited 4 packages in 1.796s

found 0 vulnerabilities

Step 2: Update the code to include json2csv and look for the base file stream module (fs).

'use strict'

const fs = require(‘fs’)

const { Parser } = require(‘json2csv’)

Step 3: Define what the titles in CSV should look like.

'use strict'

const fs = require(‘fs’)

const { Parser } = require(‘json2csv’)

const someData = [{

‘Firstname’: ‘Besar’,

‘Lastname’: ‘Bilalli’,

“Company”: ‘solution25’

}, {

‘Firstname’: ‘Besnik’,

‘Lastname’: ‘Bilalli’,

“Company”: ‘solution25’

}, {

‘Firstname’: ‘Fatlum’,

‘Lastname’: ‘Berisha’,

“Company”: ‘solution25’

}]

const fields = Object.keys(someData[0])

Step 4: Parse the object:

'use strict'

const fs = require(‘fs’)

const { Parser } = require(‘json2csv’)

const someData = [{

‘Firstname’: ‘Besar’,

‘Lastname’: ‘Bilalli’,

“Company”: ‘solution25’

}, {

‘Firstname’: ‘Besnik’,

‘Lastname’: ‘Bilalli’,

“Company”: ‘solution25’

}, {

‘Firstname’: ‘Fatlum’,

‘Lastname’: ‘Berisha’,

“Company”: ‘solution25’

}]

const fields = Object.keys(someData[0])

const csv = new Parser({fields})

Step 5: Type in FileStream:

'use strict'

const fs = require(‘fs’)

const { Parser } = require(‘json2csv’)

const someData = [{

‘Firstname’: ‘Besar’,

‘Lastname’: ‘Bilalli’,

“Company”: ‘solution25’

}, {

‘Firstname’: ‘Besnik’,

‘Lastname’: ‘Bilalli’,

“Company”: ‘solution25’

}, {

‘Firstname’: ‘Fatlum’,

‘Lastname’: ‘Berisha’,

“Company”: ‘solution25’

}]

const fields = Object.keys(someData[0])

const csv = new Parser({ fields })

fs.writFile(‘csvfile.csv’, csv.parse(someData), function (err) {

if (err) {

console.error(err);

throw err

} else {

console.log(‘csv file is saved’);

}

})

If you want to know more about e-commerce development platforms or apps, read our blog posts for Shopify and Shopware. Our dedicated outsourcing team can be your collaborator in ecommerce.

Arrange a free consultation now!