0

I am having trouble converting the following string to an array of objects. I have tried varying combinations of json parse and stringify to no avail. Here's the code:

'{"lat": 51.25869, "lng": -0.55731},{"lat": 51.17095, "lng": -0.79179},{"lat": 51.07819, "lng": -0.72810},{"lat": 51.11948, "lng": -0.46185},'

I need it to be like this:

[
    {lat: 51.28876, lng: -0.50718},
    {lat: 51.18774, lng: -0.84672},
    {lat: 51.00566, lng: -0.78475},
    {lat: 51.08973, lng: -0.46529}
];

How can I achieve this?

0

1 Answer 1

5

It looks like you could just remove the trailing comma then add braces to get a perfectly valid JSON:

var json = "[" + myString.replace(/,$/, "") + "]";
var arr = JSON.parse(json);
Sign up to request clarification or add additional context in comments.

Comments

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.