Sojtin Sojtin - 2 months ago 12
JSON Question

How to parse not separated 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"}}'

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

Comments