0

I have a text file like the one below

{"id":819827,"nm":"Razvilka","lat":55.591667,"lon":37.740833,"countryCode":"RU"}
{"id":524901,"nm":"Moscow","lat":55.752220,"lon":37.615555,"countryCode":"RU"}
{"id":1271881,"nm":"Firozpur","lat":27.799999,"lon":76.949997,"countryCode":"IN"}

i want this data into correct json format how should i do it csv format

i am starting like this

var cells = str.split('\n').map(function (el) { return el.split(/\s+/); });

how should i move further please help

1 Answer 1

2

What you need is JSON.parse, which can convert JSON strings into JS Objects;

const string = `{"id":819827,"nm":"Razvilka","lat":55.591667,"lon":37.740833,"countryCode":"RU"}\n {"id":524901,"nm":"Moscow","lat":55.752220,"lon":37.615555,"countryCode":"RU"}\n {"id":1271881,"nm":"Firozpur","lat":27.799999,"lon":76.949997,"countryCode":"IN"}`;

var cells = string.split('\n').map(JSON.parse);

// console.log(cells);

// Convert it back into JSON string using `JSON.stringify`
cells = JSON.stringify(cells);

console.log(cells);

Sign up to request clarification or add additional context in comments.

4 Comments

Add JSON.stringify to your log of cells and you'll have answered the OP's question.
Ahh, good catch. I thought OP just wanted an JS object. Have edited it. Thanks.
thanks for answering the above json data i am using to plot a graph in js but its not working 😕
@minu you could add a new question if is related to graphs. Is there any change you want in my current answer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.