MB34 MB34 - 6 months ago 26
PHP Question

PHP json_encode - null & int values returned as strings

Why would this array:

Array
(
[EventCode] => 20140709TXAO
[ShortParticipant] => Array
(
[Address1] => null
[Address2] => null
[Address3] => null
[City] => null
[Country] => null
[Email] => kkardashian@kkardashain.com
[Employer] => TNA
[FirstName] => Kim
[LastName] => Kardashian
[PID] => 1180133
[Result] => null
[State] => null
)

)


be converted to the JSON below with json_encode? Note the null values are being converted to "null"!!! That is causing issues with my receiver.

{
"EventCode": "20140709TXAO",
"ShortParticipant": {
"Address1": "null",
"Address2": "null",
"Address3": "null",
"City": "null",
"Country": "null",
"Email": "kkardashian@kkardashain.com",
"Employer": "TNA",
"FirstName": "Kim",
"LastName": "Kardashian",
"PID": "1180133",
"State": "null"
}
}


Even the int value is being converted to a string, "1180133".

var_dump results:

array
'EventCode' => string '20140709TXAO' (length=12)
'ShortParticipant' =>
array
'Address1' => string 'null' (length=4)
'Address2' => string 'null' (length=4)
'Address3' => string 'null' (length=4)
'City' => string 'null' (length=4)
'Country' => string 'null' (length=4)
'Email' => string 'kkardashian@kkardashain.com' (length=27)
'Employer' => string 'TNA' (length=3)
'FirstName' => string 'Kim' (length=3)
'LastName' => string 'Kardashian' (length=10)
'PID' => string '1180133' (length=7)
'Result' => string 'null' (length=4)
'State' => string 'null' (length=4)


Javascript code:

function callRegStatus(eventcode, RegStatus) {
inputdata = {LogonTicket: logonticket,
data2: $.extend(true, {EventCode: eventcode},
{ShortParticipant: RegStatus})};
ok_to_proceed = false;
$.ajax({async: false
, type:'POST'
, url: REGFUNCTIONS_URL
, data: inputdata
, dataType: 'json'
, success: function(data) {
ok_to_proceed = true;
}
, error: function(jqXHR, textStatus, errorThrown) {
ok_to_proceed = false;
$("#error_message").html(jqXHR.responseText);
}
});
return ok_to_proceed;
}


EE Plugin code:

public function getRegStatus() {
$data = $_POST;
$data2 = $data["data2"];

$url = $this->server . '/RegStatus/'.$data["LogonTicket"];
$options = array(CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data2)
);
$ch = curl_init();
curl_setopt_array($ch, $options);

$RegStatusResult = curl_exec($ch);

curl_close($ch);
return $RegStatusResult;
}

Answer

Before you encode your data with JSON you can do an recursive array walk like this:

array_walk_recursive($array, function(&$item, $key) {
    if ($item == 'null') $item = NULL;
});

Note the code uses lambdas and requires PHP 5.3.0 or higher. It can however easily be refactored if the anonymous function is defined before array_walk_recursive and then passed as callback — like so array_walk_recursive($array, 'nullStrToNull');.

As for the integers being casted as strings, the json_encode() option JSON_NUMERIC_CHECK (available since PHP 5.3.3) will encode numeric strings as numbers. Should the option not be available we can use array_walk_recursive() also.

Comments