One of the odd side effects of JSON’s wide spread success is how often you’ll run across data that’s not quite json
A hello world example looks something like this
{a: "Hello World"}
If you look at that string, you might think Hey, it’s a JSON string! However, it’s not strict JSON — the a
needs to be a quoted string. Consider the output of the following.
$foo = json_decode('{a: 8531329}');
var_dump($foo); //NULL
$bar = json_decode('{"a": 8531329}');
var_dump($bar); //the object
“Not Quite JSON” exists because JSON’s elevator pitch is
We encode everything using javacsript objects
This means there’s a ton of JSON code out there that parses and produce things that are valid javascript, but not valid JSON. Unfortuntly, PHP’s json_encode/json_decode
can’t handle things that aren’t valid JSON.
The colinodell/json5 Packagist package seems to solve this for PHP. I’m not familiar with the JSON5 format it implements, but so far it has parsed all the almost, but not quite, JSON strings I’ve thrown at it.