0

I have a socket connected to a third party and I receive data using websockets. At times data received through socket could have a string containing multiple sets of array.

How to handle this while parsing?

for ex, data I receive normally:

[{a:1}, {b:2}, {c:3}]

at times, I receive:

[{a:1}, {b:2}, {c:3}][{d:1}, {e:2}, {f:3}]

and I get below error, because i guess JSON.parse does not know what to do with it:

Uncaught SyntaxError: Unexpected token [ in JSON at position 267
at JSON.parse (<anonymous>)

I would want to split and parse both json array. so my new output would be:

[{a:1}, {b:2}, {c:3}] as arr[0] and  [{d:1}, {e:2}, {f:3}] as arr[1]

1 Answer 1

1

If all you want is to separate the string into an array:

var str = "[{a:1}, {b:2}, {c:3}][{d:1}, {e:2}, {f:3}]";
var res = str.replace("][", "]|[");
var arr = res.split("|");
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.