Sojtin Sojtin - 1 year ago 70
JSON Question

How to parse not separated (invalid) json string?

I want to parse some json response into php array, the problem is nginx push stream module response with not separated json string, is possible to parse this without using regex?

'{"id":1,"channel":"1","text":"Hello World!"}{"id":2,"channel":"1","text":"Hello World!"}{"id":2,"channel":"1","text":{"key_x": "value_x"}}'


The real issue was that nginx push-stream module send archive in stream, so thats why there is no separator between json data in my snippet.

Answer Source
$str = '{"id":1,"channel":"1","text":"Hello World!"}{"id":2,"channel":"1","text":"Hello World!"}{"id":2,"channel":"1","text":"Hello World!"}';

$str = str_replace('}{', '},{', $str);
$str = '[' . $str . ']';