javascript - convert csv file data in json data -
i use csvtojson convertor convert in json format.
var csvfilename = path; //path== filepath var csvconverter = new converter(); csvconverter.on("end_parsed", function (jsonobj) { console.log('in json object', jsonobj); }); csvconverter.from(csvfilename);
i use bulk export , import of data in csv file.if import csv file without changing exported csv file csvtojson parser parse data in json form correctelly .like
{ csvrows: [ { id: '51f21e7c60c058bc54000001', name: 'dummy product', alias: 'dummy-product1111111111', description: 'lorem ipsumuuu', images: '', price: '55', compare_price: 'undefined', collections: 'undefined', brand: 'undefined', quantity: 'undefined', sku: 'undefined', barcode: 'undefined', categories: '', publish: '1', variants: undefined, state: undefined, avg_rating: undefined, num_reviews: undefined, weight: undefined, free_product: undefined, option_set: undefined }]
but when modify csv file , export csvtojson parser parser data in formate -
{ csvrows: [ { { 'id\tname\talias\tdescription\timages\tprice\tcompare_price\tcollections\tbrand\tquantity\tsku\tbarcode\tcategories\tpublish\tvariants\tstate\tavg_rating\tnum_reviews\tweight\tfree_product\toption_set': '525ba1b3f96404a56a000006\tbraclet12\tbraclet12\tundefined\t\t100\tundefined\tundefined\tundefined\tundefined\tundefined\tundefined\tundefined\t\t\t\t\t\t\t\t' }];
is there way parse csv file data json form correctelly.
thanks.
you can use following js file via terminal. think it's self explanatory. if not, please write comment , i'll explain.
'use strict'; var fs = require('fs'), args = process.argv.slice(2), seperator = ',', input = fs.readfilesync('input.csv', {encoding: 'utf-8'}), lines = input.split('\n'), output = [], props = lines.shift().trim().split(seperator), i, j, values, obj; (i = 0; < args.length - 1; i++) { if (args[i] === '-s' || args[i] === '--seperator') { seperator = args[i + 1]; } } (i = 0; < lines.length; i++) { if (lines[i].length > 0) { obj = {}; values = lines[i].split(seperator); (j = 0; j < props.length; j++) { obj[props[j]] = values[j]; } output.push(obj); } } fs.writefilesync('output.json', json.stringify(output), {encoding: 'utf-8'});
Comments
Post a Comment