Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to convert a string to array of object in nodeJS

Hi all I have one string which I need to convert in array of object in node JS.
as :

var result = "[{'path': '/home/media/fileyear.jpg', 'vectors': [0.1234, 0.457, 0.234]}, 
{'path': '/home/media/fileyear2.jpg', 'vectors': [0.134, 0.557, 0.634]}]"

to

[{'path': '/home/media/fileyear.jpg', 'vectors': [0.1234, 0.457, 0.234]}, {'path': '/home/media/fileyear2.jpg', 'vectors': [0.134, 0.557, 0.634]}]

How can I do that using typescript or javascript in nodeJs.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

var jsonString = "[{'path': '/home/media/fileyear.jpg', 'vectors': [0.1234, 0.457, 0.234]}, {'path': '/home/media/fileyear2.jpg', 'vectors': [0.134, 0.557, 0.634]}]";

// Replacing single quotes with double quotes

var correctedString = jsonString.replace(/'/g, '"');

// Then Parse the corrected JSON string into an object

var result = JSON.parse(correctedString);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading