Sojtin Sojtin - 2 months ago 8
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"}}'


Edit

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
$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 . ']';

print_r(json_decode($str));

https://3v4l.org/BNVTg