1

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?

3 Answers 3

2

do not create your JSON string by hand. PHP has an excellent function http://php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

2 Comments

but what if someone wants to add extra data to the JSON string? json_encode wouldn't be a viable option.
@denoise decode the string, add new values and encode to get the new JSON string
1

Don't manually cobble JSON together!

$data = array('uid' => '0123465', 'name' => 'John Smith', 'nullValue' => null);
$json = json_encode($data);

2 Comments

Well, there are situations where using json_encode isn't feasable and encoding it "manually" let you to stream it while extracting datas
Agreed. But then one should know what one is doing. ;) I'd also still use json_encode to encode individual values or sub-objects with the correct syntax.
1

you can do some check:

$null_value = null;
if(strlen($null_value) < 1)
    $null_value = 'null';//quote 'null' so php deal with this var as a string NOT as null value
$json_string = '{"uid":0123465,"name":"John Smith","nullValue":'.$null_value.'}';
echo $json_string;

or you can quote the value null at the beginning:

$null_value = 'null';
$json_string = '{"uid":0123465,"name":"John Smith","nullValue":'.$null_value.'}';
echo $json_string;

but the prefered way to do it is to collect values in an array then encode it:

$null_value = null;
$json_string = array("uid"=>0123465,"name"=>"John Smith","nullValue"=>$null_value);
echo json_encode($json_string,JSON_FORCE_OBJECT);

1 Comment

The problem with doing $null_value = 'null', is that it turns the value into a string, and the receiver is not expecting a string value which then returns an error. But the json_encode is correct, not sure why I forgot about this method. Thank you for the comprehensive answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.