json_decode fails on godaddy hosting

asked 1 year ago by Aleks G

I use json_decode in my PHP to parse data from POST (the POST itself is executed via ajax). My code looks like this:

$data = json_decode($_POST['logdata'], true);
if(!$data) {
    $ecodes = array(
    JSON_ERROR_NONE           => "No error has occurred",
    JSON_ERROR_DEPTH          => "The maximum stack depth has been exceeded",
    JSON_ERROR_STATE_MISMATCH => "Invalid or malformed JSON",
    JSON_ERROR_CTRL_CHAR      => "Control character error, possibly incorrectly encoded",
    JSON_ERROR_SYNTAX         => "Syntax error",
    JSON_ERROR_UTF8           => "Malformed UTF-8 characters, possibly incorrectly encoded"
    );
    $err = json_last_error();
    $result = array("error" => 1, "error_msg" => "Invalid log data: " . $ecodes[json_last_error()]);
    echo json_encode($result);
    exit;
}

...  //use $data array

This code works fine. When executed on my local linux machine or my mac, the passed data is decoded correctly and everything works fine. However when the same code is executed on godaddy shared hosting, the decoding fails with Syntax error. Having spent a lot of time trying to narrow down the problem, I found out that the problem occurs when I have < or > characters in the values of JSON object properties.

The uploading part (ajax) in javascript looks like this:

$.ajax({
    url:        '/emaillog.php',
    type:       'POST',
    dataType:   'json',
    data:       { logdata: JSON.stringify(this.logData[this.scenarioLogId]) },
    cache:      false,
    error:      function(jqXHR, textStatus, errorThrown) {
        alert("Error occurred during upload: " + textStatus);
    },
    success:    function(data, textStatus, jqXHR) {
        var msg = data.error
                  ? "An error occurred processing action log: " + data.error_msg
                  : "Action log processed successfully.";
        alert(msg);
    }
})

As you can see, the value of the POST parameter is created using JSON.stringify and is correct on inspection (both client and server side). If it matters, here's an example value of the submitted POST parameters:

{
    "scenarioId":"1",
    "scenarioName":"MOH",
    "startTime":1355496349,
    "log":[
        {
         "role":"Leader",
         "task":"MOH",
         "response":"start",
         "time":1355496349
        },
        {
         "role":"Head",
         "task":"<span class=\"bold\">Assign Role</span>",
         "response":"done",
         "time":1355496351
        }
    ]
}

If the last element in the array (with task Assign Role) is removed, then everything is processed successfully. If I replace < and >> characters with spaces, again, everything is fine.

Two questions arise from this:

  1. If syntax error on such a JSON string is the correct behaviour, then why do I not see the error in the other two environments? Also, why is JSON.stringify then producing an incorrect string?

  2. If syntax error on such a JSON string is an incorrect behaviour, then something must be wrong with godaddy's PHP implementation.

Anybody can shed some light on this situation?


3 Answers:


  • I'm not sure if this is it or not, but if you're pulling the data from a database then you have to set the utf8 encoding when inserting it into the database in order to prevent json from dropping pieces of it.

    Before the insert:

    $text = utf8_encode($text);
    

    If that's not exactly what you are doing I'm willing to bet the answer is really close to that.

    answered 1 year ago by Ben

  • JSON or JavaScript Object Notation, is syntax for storing and exchanging text information, much like XML.

    To enable the JSON extension on your hosting account, you must modify the php5.ini file to include the following code:

    extension=json.so
    
    answered 1 year ago by seyz4all

  • For whatever reason, on godaddy hosting only (maybe in some other places too, but not in other places I tested), when I was submitting the form, the server was escaping quotes - and that's what was causing the issue. The solution to the problem was quite simple: instead of doing

    $data = json_decode($_POST['logdata'], true);
    

    I did

    $data = json_decode(stripslashes($_POST['logdata']), true);
    

    and everything worked perfectly.

    answered 1 year ago by Aleks G

You must log in to answer this question.


Viewed: 285 times

Last updated: Dec 22, 2012

Related questions