Skip to content

Instantly share code, notes, and snippets.

@rik
Forked from codepo8/CSVtoJSon.js
Last active February 29, 2024 09:12
Show Gist options
  • Save rik/c45d2eec52ebfb78be5f5eedeb0ea617 to your computer and use it in GitHub Desktop.
Save rik/c45d2eec52ebfb78be5f5eedeb0ea617 to your computer and use it in GitHub Desktop.

Revisions

  1. rik revised this gist Feb 29, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions CSVtoJSon.js
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,7 @@ function _csvRowToObject(headers, row) {
    function csvToJSON(csv) {
    const [headersRow, ...rows] = csv.split('\n')
    let headers = _csvRowToArray(headersRow)
    const result = rows.map(row => _csvRowToObject(headers, row))
    return result
    return rows.map(row => _csvRowToObject(headers, row))
    }

    let csv = `Name, Age, City
  2. rik revised this gist Feb 29, 2024. 1 changed file with 19 additions and 24 deletions.
    43 changes: 19 additions & 24 deletions CSVtoJSon.js
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,25 @@
    const csvToJSON = (csv) => {
    const getcsvdata = (csv) => {
    const csvRegex = /,(?=(?:(?:[^"]*"){2})*[^"]*$)/;
    const trimQuotes = /^"|"$/g;
    csv = csv.split(csvRegex).map(
    h => h.trim().replace(trimQuotes, '')
    );
    return csv;
    }
    let lines = csv.split('\n');
    let headers = getcsvdata(lines[0]);
    let result = [];
    lines.slice(1).forEach(line => {
    var item = {};
    let currentline = getcsvdata(line);
    headers.forEach((header, i) => {
    item[header] = currentline[i];
    })
    result.push(item);
    });
    return result;
    function _csvRowToArray(csv) {
    const csvRegex = /,(?=(?:(?:[^"]*"){2})*[^"]*$)/
    const trimQuotes = /^"|"$/g
    return csv.split(csvRegex).map(h => h.trim().replace(trimQuotes, ''))
    }

    let csv = `Name, Age, City
    function _csvRowToObject(headers, row) {
    let currentRow = _csvRowToArray(row)
    return Object.fromEntries(headers.map((header, i) => [header, currentRow[i]]))
    }

    function csvToJSON(csv) {
    const [headersRow, ...rows] = csv.split('\n')
    let headers = _csvRowToArray(headersRow)
    const result = rows.map(row => _csvRowToObject(headers, row))
    return result
    }

    let csv = `Name, Age, City
    John, 21, New York
    "Jane, Doe", 22, San Francisco
    Jim, 23, Chicago
    "Jill Chill", 24, "Tacoma, Seattle"`;
    "Jill Chill", 24, "Tacoma, Seattle"`

    console.log(csvToJSON(csv));
    console.log(csvToJSON(csv))
  3. @codepo8 codepo8 revised this gist Feb 28, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CSVtoJSon.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ const csvToJSON = (csv) => {
    const csvRegex = /,(?=(?:(?:[^"]*"){2})*[^"]*$)/;
    const trimQuotes = /^"|"$/g;
    csv = csv.split(csvRegex).map(
    h => h.trim().replace(trimquotes, '')
    h => h.trim().replace(trimQuotes, '')
    );
    return csv;
    }
  4. @codepo8 codepo8 revised this gist Feb 28, 2024. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions CSVtoJSon.js
    Original file line number Diff line number Diff line change
    @@ -22,9 +22,9 @@ const csvToJSON = (csv) => {
    }

    let csv = `Name, Age, City
    John, 21, New York
    "Jane, Doe", 22, San Francisco
    Jim, 23, Chicago
    "Jill Chill", 24, "Tacoma, Seattle"`;
    John, 21, New York
    "Jane, Doe", 22, San Francisco
    Jim, 23, Chicago
    "Jill Chill", 24, "Tacoma, Seattle"`;

    console.log(csvToJSON(csv));
  5. @codepo8 codepo8 created this gist Feb 28, 2024.
    30 changes: 30 additions & 0 deletions CSVtoJSon.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    const csvToJSON = (csv) => {
    const getcsvdata = (csv) => {
    const csvRegex = /,(?=(?:(?:[^"]*"){2})*[^"]*$)/;
    const trimQuotes = /^"|"$/g;
    csv = csv.split(csvRegex).map(
    h => h.trim().replace(trimquotes, '')
    );
    return csv;
    }
    let lines = csv.split('\n');
    let headers = getcsvdata(lines[0]);
    let result = [];
    lines.slice(1).forEach(line => {
    var item = {};
    let currentline = getcsvdata(line);
    headers.forEach((header, i) => {
    item[header] = currentline[i];
    })
    result.push(item);
    });
    return result;
    }

    let csv = `Name, Age, City
    John, 21, New York
    "Jane, Doe", 22, San Francisco
    Jim, 23, Chicago
    "Jill Chill", 24, "Tacoma, Seattle"`;

    console.log(csvToJSON(csv));