Invalid JSON parsing using PHP

  1. All the quotes should be double quotes " and not single quotes '.
  2. All the keys should be quoted.
  3. The whole element should be an object.
    function my_json_decode($s) {
        $s = str_replace(
            array('"',  "'"),
            array('\"', '"'),
            $s
        );
        $s = preg_replace('/(\w+):/i', '"\1":', $s);
        return json_decode(sprintf('{%s}', $s));
    }

Leave a Comment