I have json data represented like this
{key:\"value\"}
(no quotes arround key...)
I want to translate it to an associative array.
This worked for me, using regex replace '/\s(\w+)\s/i'
$json = file_get_contents("php://input"); // or whatever json data
$json = preg_replace('/\s(\w+)\s/i', '"$1"', $json);
$json = json_decode($json, true);
Please do not use regexps to do this! JSON grammar cannot be correctly parsed this way by definition. You will open yourself to a ton of future bugs.
I recommend using a YAML parser, because YAML is a superset of JSON and allows unquoted literals at the same time.
Symfony YAML component works great.
There will be a performance penalty in comparison to json_decode
which is implemented natively.