I have json data represented like this
{key:\"value\"}
(no quotes arround key...)
I want to translate it to an associative array.
If you can't turn that into valid JSON at the source, then you can use Services_JSON from PEAR to parse it, since adding quotes around the key is a non-trivial error-prone process.
Services_JSON
will correctly parse the invalid key string.
Example:
$json = new Services_JSON();
var_dump($json->decode('{key:"value"}'));
Output:
object(stdClass)#2 (1) {
["key"]=>
string(5) "value"
}
To avoid that double quotes are inserted in places where they shouldn't, you should skip those quoted strings in this manipulation.
For instance, if you have this JavaScript object literal in a string:
{
dt:"2016-10-22T09:13:20",
"x:y":false
}
... then care must be taken not to have 22T09:
change into "22T09":
. Also the already quoted key, "x:y"
should stay unaltered.
You could use this regular expression for achieving that:
preg_replace('/("(.*?)"|(\w+))(\s*:\s*(".*?"|.))/s', '"$2$3"$4', $text);
JavaScript object literals allow numeric constants with left-padded zeroes, like 001
, and/or with the unary +
sign, which are neither allowed in JSON. To remove those offending characters also, you could use this extended version:
preg_replace('/("(.*?)"|(\w+))(\s*:\s*)\+?(0+(?=\d))?(".*?"|.)/s', '"$2$3"$4$6', $text);
$results = \Symfony\Component\Yaml\Yaml::parse("{a: d, b: 'c', e: [a, 3]}");
You probably can only use that lib without having to use the whole Symfony package : https://packagist.org/packages/symfony/yaml
As per the documentation (see Example #3 - 'common mistakes using json_decode'), keys must be enclosed in double quotes.
Where are you getting the JSON data?
REGEX is totally not recommended for such cases, don't use that for parsing such data. If you have a simple goals and want to avoid the stable PEAR package
, then you might try JSON-php
library (but no-longer maintained).
1) Get JSON.phps
file from here and rename to .php
and replace constructor function names to __construct
.
2) usage:
$content = '{myKey:"valueeeee"}';
include(__DIR__.'/JSON.php');
$this->json = new Services_JSON( SERVICES_JSON_LOOSE_TYPE ); // to return objects instead of Associative array, remove the argument
var_dump( $this->json->decode($content) );
You can either fix the JSON at the source so that it returns a valid JSON structure, or you can manually add quotes around the keys.
This answer to a similar question has an example of how to do that:
function my_json_decode($s) {
$s = str_replace(
array('"', "'"),
array('\"', '"'),
$s
);
$s = preg_replace('/(\w+):/i', '"\1":', $s);
return json_decode(sprintf('{%s}', $s));
}