CSV to JSON

Simple CSV to JSON parser. designed to be accessed as an object so headers are sanitised

by rity

HTML

<div id="test">
  Prenosni računari
    Laptop računari
        Cenovni rang
            20.000  - 30.000 (8)
            30.000  - 40.000 (44)
            40.000  - 50.000 (35)
            50.000  - 60.000 (19)
            60.000  - 70.000 (23)
            70.000  - 80.000 (18)
            80.000  - 90.000 (19)
            90.000  - 100.000 (14)
            100.000  - 110.000 (17)
            110.000  - 120.000 (10)
            120.000  - 130.000 (12)
            130.000  - 140.000 (10)
            140.000  - 150.000 (9)
            150.000  - 160.000 (16)
            preko 160.000 (66)
        Proizvođač
            Acer(7)
            Apple(32)
            Asus(108)
            Dell(34)
            HP(49)
            Lenovo(53)
            Toshiba(37)
        Namena
            Bussines(40)
            Gaming(53)
            Home(50)
            Multimedia(172)
        Dijagonala ekrana
            10.1"(7)
            11.6"(11)
            12"(13)
            12.5"(5)
            13.3"(46)
            14"(14)
            15.4"(2)
            15.6"(172)
            17.3"(45)
        Procesor
            AMD A6(2)
            Intel Atom(11)
            Intel Celeron(27)
            Intel Core i3(50)
            Intel Core i5(75)
            Intel Core i7(107)
            Intel Core M(21)
            Intel Pentium(22)
        Memorija
            1 GB(1)
            2 GB(13)
            4 GB(126)
            6 GB(8)
            8 GB(129)
            12 GB(6)
            16 GB(30)
            32 GB(2)
        Hard Disk1
            1 TB + 8 GB SSHD(9)
            500 GB + 16 GB SSHD(1)
            500 GB + 32 GB SSHD(2)
            500 GB + 8 GB SSHD(10)
            32 GB eMMC(5)
            64 GB eMMC(5)
            128 GB SSD(23)
            256 GB SSD(52)
            500 GB(81)
            512 GB SSD(16)
            750 GB(4)
            1 TB(101)
            2 TB(6)
        Grafika
            AMD(52)
            Nvidia(112)
            Integrisana(151)
     ...

JavaScript

//var csv is the CSV file with headers
function csvJSON(csv) {
    var i;
    var j;
    //this could be changed to replace with just "'"
    var input = csv.replace(/\"\"/g, encodeURIComponent('"'));
    //split on " to create an odds in quotes
    var quotesAndValues = input.split(/\"/g);
    var escapedInput;

    var quotesAndValuesLength = quotesAndValues.length;
    //encode the odd positions as these should be treated as one value
    //and need to ignore , 
    for (i = 1; i < quotesAndValuesLength; i = i + 2) {
        quotesAndValues[i] = encodeURIComponent(quotesAndValues[i]);
    }
    //join together the newly escaped values with no gaps
    escapedInput = quotesAndValues.join("");
    //split at new lines to get each row
    var lines = escapedInput.split(/\r\n|\n/g);

    var result = [];
    //split index 0 at , to get headers
    var headers = lines[0].split(/,/g);
    var headersLength = headers.length;
    for (i = 0; i < headersLength; i++) {
        //Headers will be JS objects so replace special char with safe _
        headers[i] = headers[i].replace(/\W/g, '_');
    }

    for (i = 1; i < lines.length; i++) {

        var obj = {};
        //splitat , to get values
        var currentline = lines[i].split(/,/g);
        var headersLength = headers.length;
        for (j = 0; j < headersLength; j++) {
            //double decode
            //first: decodes the quoted values , % etc
            //second: decodes the double quotes that were escaped at the start as %22 (%2522)
            //this may not be performant 
            obj[headers[j]] = decodeURIComponent(decodeURIComponent(currentline[j]));
        }

        result.push(obj);

    }

    //return result; //JavaScript object
    return JSON.stringify(result); //JSON
}

var test = document.getElementById('test');
test.innerHTML = csvJSON('1,2,3,4-a,!£$%^&*()5\na,b,c,"d,e,f",""g\r\n1,2,3,4,5');