String to JSON array using Node
String to JSON array using Node
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:
JSON.parse(string)
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.
console.log(a)
['Phonetype':'Pre','Phone':'918282311','Phonetype':'pre','Phone':'918333222']
@Li357 — It's entirely possible that the OP intended to ask for that though. Their question doesn't really make sense in its current form.
– Quentin
Sep 16 '18 at 17:52
If you want the data as a JavaScript array (rather than a JSON array), why not use a CSV library designed to just parse CSV? Why convert it to JSON and then convert it (again) to JavaScript?
– Quentin
Sep 16 '18 at 17:53
"But I got the following error" — I don't get that error: jsbin.com/joharosehe/1/edit?js,console — perhaps
string
doesn't contain the value you think it does.– Quentin
Sep 16 '18 at 17:55
string
Please read What is the difference between JSON and Object Literal Notation and update your question accordingly as it is really unclear. JSON is a string by definition.
– str
Sep 16 '18 at 17:56
2 Answers
2
If it contains 'single quotes' instead of "double quotes" then it is not valid JSON.
You will need to do a .replace(/'/g, '"')
console.log(JSON.parse("['Phonetype':'Pre','Phone':'918282311','Phonetype':'pre','Phone':'918333222']".replace(/'/g, '"')));
JSON.parse works fine on that string.
console.log(JSON.parse('["Phonetype":"Pre","Phone":"918282311","Phonetype":"pre","Phone":"918333222"]'));
Your data is already an object and doesn't need parsing, try
csv().fromFile(csvFilePath)
.then((jsonObj)=>
var a = jsonObj[0].phoneNumber;
console.log(a);
console.log(a[0].Phonetype);
console.log(a[0].Phone);
console.log(a[1].Phonetype);
console.log(a[1].Phone);
);
I have updated the string. String contains single quotes instead of double of quotes.
– Anil Kumar
Sep 16 '18 at 18:20
console.log(a[0]) prints only [ in the console. It is printing the character in the 0th position
– Anil Kumar
Sep 16 '18 at 18:22
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
If that is a string then it is a JSON array already.
– Quentin
Sep 16 '18 at 17:50