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 add comma after each object and convert into array in javascript react js

how can i add comma after each object and convert into array. here’s my response data

{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}

i want this

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

[{"url":"example.com/john","age":"32"},{"url":"example.com/mike","age":"42"}]

i tried with regex but it’s converting data into string but i want to render this data

>Solution :

How about a little bit of Regex magic to make it valid JSON.

const res = '{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}';

const betterJson = `[${res.replace(/}{/g,'},{')}]`;
console.log(JSON.parse(betterJson))
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