Search code examples
jqueryjsonparse-error

Invalid json has suddenly started throwing parse errors


There are two places where I have observed this. Both are AJAX calls which return some JSON which is used to populate the options of two select dropdowns. I am currently working on applying enhancements to these dropdowns using jquery's chosen plugin and I guess some change is causing this problem. I am not sure what exactly, because I reverted the changes for that module where I am getting the parse error, but the error is still happening.

So, I am asking, in general what could be the reasons behind invalid JSON suddenly causing problem. Please note that the returned JSON has always been invalid. I have made no changes to this. Also, I did not make any changes to the $.ajax calls. Sample of the JSON -

{
 "result":"success",
"reqparams": 
{"site_id": {"name":"site_id","display_name":"","possible_values":"","default_value":"","editable":"1","description":"the ad slot id provided by your 4th Screen account manager",},},
"optparams": {
"keywords": {"name":"keywords","display_name":"","possible_values":"","default_value":"","editable":"1","description":"",},
"reachability": {"name":"reachability","display_name":"","possible_values":"wifi[ADMARVEL_SEP]cell","default_value":"","editable":"1","description":"Information about how the device is being connected to the internet.",},
},
}

Note: the extra commas, e.g. after "description" attribute's value.

Updates Code which generates JSON - sample code - please ignore the fact that json_encode is not being used. it is very old code.

if (is_array($reqd_params) && count($reqd_params) > 0)
        {
            header("HTTP/1.1 200 OK");
            echo '{
                    "result":"success",
                    "reqparams": {';
            foreach ($reqd_params as $paramKey => $paramValArr)
            {
                echo '"'.$paramValArr['name'].'": {';
                echo '"name":"'.$paramValArr['name'].'",';
                echo '"display_name":"'.$paramValArr['display_name'].'",';
                echo '"possible_values":"'.$paramValArr['possible_values'].'",';
                echo '"default_value":"'.$paramValArr['default_value'].'",';
                echo '"editable":"'.$paramValArr['editable'].'",';
                echo '"description":"'.addslashes($paramValArr['description']).'",';
                echo '},';
            }
            echo '}';

            if(!empty($reportData) && $reportData != -1)
            {
                echo',';
                echo '"reportData":"'.htmlspecialchars($reportData).'"';
            }

            echo ' }';

        }

Client side code

$.ajax({
            type: "POST",
            url: posturl,
            data : data,
            dataType: "json",
            async: false,
            success: function(msg){

                         //not coming here
                        },

                        error: function(jqXHR, textStatus, errorThrown){
                alert("error "+errorThrown);

                                //alerts parse error
            }
                });

Solution

  • I would say that this fact

    I upgraded from jquery 1.3.2 to 1.6.4.
    

    (from the comments)

    and the fact that your jquery code used to accept incorrect JSON (as in: it was doing something "wrong") but now works as expected, are the source of the seen behaviour, as your question was.

    As a sollution, in the case changing the code REALLY isn't an option, you could just change the datatype for the expected result, and go manually parse your specialJSON code.