Every line of 'csvtojson' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.
61 export function convertCSVToJSON(csv, keySeparator = ',', lineSeparator = '\r\n', indent = 2) { 62 if (!csv) { 63 return ''; 64 } 65 /** @type {?} */ 66 const csvArray = csv.split(lineSeparator); 67 // Input CSV must have a minimum of two rows 68 if (csvArray.length < 2) { 69 return ''; 70 } 71 /** @type {?} */ 72 const newObjects = []; 73 // Extract object keys from header row 74 /** @type {?} */ 75 const keys = csvArray[0].split(keySeparator); 76 // Iterate through array, creating one output line per object 77 for (let i = 1; i < csvArray.length; i++) { 78 /** @type {?} */ 79 const newObject = {}; 80 /** @type {?} */ 81 const values = csvArray[i].split(keySeparator); 82 if (values.length !== keys.length) { 83 continue; 84 } 85 for (let j = 0; j < keys.length; j++) { 86 newObject[keys[j]] = values[j]; 87 } 88 newObjects.push(newObject); 89 } 90 return formatJSON(newObjects, indent); 91 }
Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code
38 csvStringToJson(csvString) { 39 return this.csvToJson(csvString); 40 }
26 function convert(csv: any, separator: string): any { 27 const lines = csv.split('\n'); 28 const result: any[] = []; 29 const headers = lines[0].split(separator); 30 lines.splice(0, 1); 31 32 lines.forEach((line: any) => { 33 const obj = {}; 34 const currentline = line.split(separator); 35 headers.forEach((header: any, i: any) => { 36 obj[header] = currentline[i]; 37 }); 38 result.push(obj); 39 }); 40 41 return result; 42 }
95 function arrayToCSV(rows) { 96 var content = ""; 97 rows.forEach(function(row, index) { 98 content += row.join(",") + "\n"; 99 }); 100 return content; 101 }