I have a system that sends and receives all data as JSON strings, and therefore must format all the data I need to send to it as a JSON string.
I am receiving values from a form using a PHP POST call, then using these values to create a string in a JSON format. The problem is with NULL values as well as true and false values. When these values are included in the string from the POST values it simply leaves it blank, but the JSON formats a NULL value as the text null.
See the example below:
<?php
$null_value = null;
$json_string = '{"uid":0123465,"name":"John Smith","nullValue":'.$null_value.'}';
echo $json_string;
//output
{"uid":0123465,"name":"John Smith","nullValue":}
?>
However, the correct output I need is:
$json_string = '{"uid":0123465,"name":"John Smith","nullValue":null}';
echo $json_string;
//output
{"uid":0123465,"name":"John Smith","nullValue":null}
?>
My question is, how can I get PHP null values to appear correctly as JSON null values, rather than just leaving it empty? Is there a method for converting them?