Skip to content

Instantly share code, notes, and snippets.

@lcllarsen
Last active November 19, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcllarsen/4f2dc46198e60bfa1c5c2c7014c20092 to your computer and use it in GitHub Desktop.
Save lcllarsen/4f2dc46198e60bfa1c5c2c7014c20092 to your computer and use it in GitHub Desktop.
const csv = require('fast-csv');
const xml = require("js2xmlparser");
const fs = require('fs');
const doc = [];
const options = {
declaration:{
include: false
}
}
const stream = fs.createReadStream("input.csv");
csv
.fromStream(stream, {delimiter : ';', headers: true})
.on("data", function(data){
let name = data.name;
let content = data.content;
let review_url = data.review_url;
let rating = data.rating;
let product_url = data.product_url;
let review_timestamp = data.review_timestamp;
let obj = {
"reviewer": {
"name": name
},
"review_timestamp": review_timestamp,
"content": content,
"review_url": {
"@": {
"type": "singleton"
},
"#": review_url
},
"ratings": {
"overall": {
"@": {
"min": 1,
"max": 5
},
"#": rating
}
},
"products": {
"product": {
"product_url": product_url;
}
}
};
doc.push(obj);
})
.on("end", function(){
doc.forEach(function(row){
fs.appendFile('output.xml', xml.parse("review", row, options) + '\n', 'utf-8', (err) => {
if (err) throw err;
});
});
fs.appendFile('output.xml', "</reviews></feed>", (err) => {
if(err) throw err;
});
});
@JLPCM
Copy link

JLPCM commented Nov 19, 2022

Hi, I tried to add this code to my Shopify store, by adding a new .js file in Assets but I can't see if it works...
What is the xml URL?
My codding knowledge is very limited.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment