This question is similar to string-to-json-array-of-json-objects. I have the following string:
"[{'Phonetype':'Pre','Phone':'918282311'},{'Phonetype':'pre','Phone':'918333222'}]"
How to convert this string to an array of objects using node?
Am reading data from a CSV file using csvtojson. Excel sheet contains a field phoneNumber. This field contains the above mentioned data. Am getting this data as string from the csv file. I want the data as JSON.
I tried JSON.parse(string)
. But I got the following error:
Unhandled rejection SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
Code:
csv().fromFile(csvFilePath)
.then((jsonObj)=>{
var a = jsonObj[0].phoneNumber;
console.log(a);
console.log(JSON.parse(a));
});
Here console.log(a)
prints [{'Phonetype':'Pre','Phone':'918282311'},{'Phonetype':'pre','Phone':'918333222'}]
as a string. When I try to convert into array of objects in the next line, it throws exception.