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']






If that is a string then it is a JSON array already.

– Quentin
Sep 16 '18 at 17:50






@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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế