Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chason's avatar

Best way xml to json

Hi guys,

What is the best practice convert xml file to json in php (laravel)?

If have tried:

$sxml = simplexml_load_file($this->filename); dd(json_encode($sxml));

Thanks!

0 likes
5 replies
ohffs's avatar

That should work I think - what problem do you have?

joedawson's avatar

Usually like this

$xml  = simplexml_load_string($xml);
$json = json_encode($xml);
$json = json_decode($json, true);

return $json;

Please or to participate in this conversation.