javascript split and JSON.parse

45,695

You don't have any JSON, so don't use JSON.parse. Once you split you already have an array whose elements could be used directly:

var data = "abc, xyz, pqr";
var data_array = data.split(',');
alert(data_array[0]);

and if you want to convert this array to a JSON string you could do this:

var json = JSON.stringify(data_array);
alert(json);
Share:
45,695
gautamlakum
Author by

gautamlakum

I help startups and enterprises go ahead with their digital products. Product Designer. Design Sprint Facilitator.

Updated on April 10, 2020

Comments

  • gautamlakum
    gautamlakum about 4 years

    I want to parse array in JSON format using javascript. I have written following code.

    var data = "abc, xyz, pqr";
    var data_array = data.split(',');
    
    var data_parsed = JSON.parse(data_array);
    alert(data_parsed);
    

    It gives me the error of JSON.parse I have no idea how to resolve this javascript error.