Sample code:
<?php $json = "['foo', 'bar']"; var_dump( json_decode($json) );
It works with PHP 5.5.3 but it fails for lower PHP's versions
It works on my machine with PHP 5.5.3 but it fails everywhere else.
I know it is incorrect JSON but my webservice gives me JSON with '
symbols together with "
['foo', "bar", {'test': "crazy \"markup\""}]
Sandbox
How to parse JSON data with apostrophes in PHP 5.3? Obviously original JSON I want to parse is more complex.
(I can't upgrade my PHP on production server neither get proper JSON from webservice)
Here's an alternative solution to this problem:
function fixJSON($json) { $regex = <<<'REGEX' ~ "[^"\\]*(?:\\.|[^"\\]*)*" (*SKIP)(*F) | '([^'\\]*(?:\\.|[^'\\]*)*)' ~x REGEX; return preg_replace_callback($regex, function($matches) { return '"' . preg_replace('~\\\\.(*SKIP)(*F)|"~', '\\"', $matches[1]) . '"'; }, $json); }
This approach is more robust than h2ooooooo's function in two respects:
\"
, for which h2o's version seems to go into an infinite loop.Test:
$brokenJSON = <<<'JSON' ['foo', {"bar": "hel'lo", "foo": 'ba"r ba\"z', "baz": "wor\"ld ' test"}] JSON; $fixedJSON = fixJSON($brokenJSON); $decoded = json_decode($fixedJSON); var_dump($fixedJSON); print_r($decoded);
Output:
string(74) "["foo", {"bar": "hel'lo", "foo": "ba\"r ba\"z", "baz": "wor\"ld ' test"}]" Array ( [0] => foo [1] => stdClass Object ( [bar] => hel'lo [foo] => ba"r ba"z [baz] => wor"ld ' test ) )
Here's a simple parser that'll fix your quotes for you. If it encounters a '
quote which isn't in a double quote "
, it'll assume that it's wrong and replace the double quotes inside of that quote, and turn the quote enclosured into double quotes:
Example:
<?php function fixJSON($json) { $newJSON = ''; $jsonLength = strlen($json); for ($i = 0; $i < $jsonLength; $i++) { if ($json[$i] == '"' || $json[$i] == "'") { $nextQuote = strpos($json, $json[$i], $i + 1); $quoteContent = substr($json, $i + 1, $nextQuote - $i - 1); $newJSON .= '"' . str_replace('"', "'", $quoteContent) . '"'; $i = $nextQuote; } else { $newJSON .= $json[$i]; } } return $newJSON; } $brokenJSON = "['foo', {\"bar\": \"hel'lo\", \"foo\": 'ba\"r'}]"; $fixedJSON = fixJSON( $brokenJSON ); var_dump($fixedJSON); print_r( json_decode( $fixedJSON ) ); ?>
Output:
string(41) "["foo", {"bar": "hel'lo", "foo": "ba'r"}]" Array ( [0] => foo [1] => stdClass Object ( [bar] => hel'lo [foo] => ba'r ) )
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With