Sanjay Achar Sanjay Achar - 6 months ago 65
JSON Question

CSV to JSON in angularjs

I am trying to convert CSV to JSON in angularjs, but I am unable to.

Here is the raw data, I receive from API -


"sentby: Sanjay, sitename: Flipkart, PinCode: 080, stdnumber: 56477382, website: https://flipkart.com, status: Done".


I want to convert this to JSON

rob rob
Answer

You could convert a csv string into an object with something like this

var csv = "sentby: Sanjay, sitename: Flipkart, PinCode: 080, stdnumber: 56477382, website: https://flipkart.com, status: Done";
console.log(csv
    .split(',')
    .reduce(function (obj, field) {
        var parts = field.split(':');
        obj[parts[0].trim()] = parts[1].trim();
        return obj;
    }, {}));
Comments