Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a file with a multiple JSONs in PHP(Laravel)?

Tags:

json

php

laravel

I have input file that looks something like this:

{"name": "foo"}{"name": "bar"}

How to parse that?

like image 237
Most31 Avatar asked Dec 15 '25 04:12

Most31


1 Answers

If you're sure, that the individual JSONs are valid, you can try to transform it into an array of JSON objects, like this:

$data = '{"name": "foo"}{"name": "bar"}';

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

// Now it's valid
// [{"name": "foo"},{"name": "bar"}]

Since }{ is always invalid in JSON, it's safe to say, that it won't affect your data.

like image 75
JuhG Avatar answered Dec 16 '25 19:12

JuhG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!