Reputation: 641
I'm writing a function which takes in the results of a web scrape that I've performed on a tshirt website.
Each tshirt, has been stored as an object, with a title, price, imgUrl, URL and time. Each of these objects is stored in an array, holding all the tshirts.
In this function, I first convert the array to a JSON string. I console log this, just to check this is being done correctly.
I then create the folder if it doesn't already exist called data.
I list the fields I want for the CSV.
I then write the file by passing in my data (JSON string), the file path, and set it to overwrite.
Right now, when I call the function, it does all it's required to, except, when I open up the CSV file, the columns are titled correctly, yet the table is completely empty.
Any ideas? I'd really appreciate your input, thank you!
Code is below:
var json2csv = require('json2csv');
var fs = require('fs');
function convertJson2Csv(){
//The scraper should generate a folder called `data` if it doesn’t exist.
var dir ='./data';
if(!fs.existsSync(dir)){
fs.mkdirSync(dir);
};
var tshirtArray = [ {
Title: 'Mike the Frog Shirt, Orange',
Price: '$25',
ImageURL: 'img/shirts/shirt-108.jpg',
URL: 'http://shirts4mike.com/shirt.php?id=105',
Date: 'September 23rd 2016, 12:28:49 am'
},{
Title: 'Logo Shirt, Teal',
Price: '$20',
ImageURL: 'img/shirts/shirt-107.jpg',
URL: 'http://shirts4mike.com/shirt.php?id=105',
Date: 'September 23rd 2016, 12:28:49 am'
},{
Title: 'Logo Shirt, Gray',
Price: '$20',
ImageURL: 'img/shirts/shirt-106.jpg',
URL: 'http://shirts4mike.com/shirt.php?id=105',
Date: 'September 23rd 2016, 12:28:49 am'
},{
Title: 'Mike the Frog Shirt, Yellow',
Price: '$25',
ImageURL: 'img/shirts/shirt-105.jpg',
URL: 'http://shirts4mike.com/shirt.php?id=105',
Date: 'September 23rd 2016, 12:28:50 am'
}];
var tshirtJson = JSON.stringify(tshirtArray);
var fields = ['Title', 'Price', 'ImageURL', 'URL', 'Date'];
var csv = json2csv({ data: tshirtJson, fields: fields });
console.log(csv);
fs.writeFile('./data/file.csv', csv, {overwrite: true}, function(err) {
if (err) throw err;
console.log('file saved');
});
};
convertJson2Csv();
Upvotes: 0
Views: 76
Reputation: 315
Probably it cannot match the fields with the object properties. I see on the tshirtArray objects you have an 'img' property and in the fields is 'imageURl'.
Try to change the object to:
var tshirtArray = [ { 'Price': '$20',
'ImageURl': 'img/shirts/shirt-107.jpg',
'Title': 'Logo Shirt, Teal',
'URL': 'http://shirts4mike.com/shirt.php?id=105',
'Date': 'September 22nd 2016, 11:17:48 pm' },
...
];
you can also make the titles pretty using passing the fieldnames:
https://github.com/zemirco/json2csv#example-4
Upvotes: 1
Reputation: 338326
Your data object has lowercase keys. Your fields
array has not.
var fields = ['Title', 'Price', 'ImageURl', 'URL', 'Date'];
JavaScript is case-sensitive. The field names must match the object keys.
Upvotes: 1