Best way to create an empty object in JSON with PHP?

To create an empty JSON object, I usually use:

json_encode((object) null); 

null listing for an object works, but is there any other preferred way and / or any problem with this solution?

+61
json php
Dec 21 '11 at 20:05
source share
4 answers

Your solution may work.

The documentation states that (object) null will result in an empty object, some may say that your code is valid and that it uses a method.

PHP: objects - a guide

If the value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty.




.. but, try to keep him safe!

Although you never know when / if the above will change, so if you want to be 100% sure that you will always have {} in your encoded data, you can use hacks, such as:

 json_encode (json_decode ("{}")); 

Despite being tedious and ugly, I assume / hope json_encode / json_decode is compatible with one and the other and will always check the following for true:

 $a = <something>; $a === json_decode (json_encode ($a)); 



Recommended Method

json_decode ("{}") will return the default stdClass using the one below, so it should be considered safe. Although, as already mentioned, this is almost the same as doing (object) null .

 json_encode (new stdClass); 
+79
Dec 21 '11 at 20:24
source share

If you use objects as dynamic dictionaries (and I suppose you do), I think you want to use ArrayObject .

It appears in the JSON dictionary even when it is empty. Great if you need to distinguish between lists (arrays) and dictionaries (associative arrays):

 $complex = array('list' => array(), 'dict' => new ArrayObject()); print json_encode($complex); // -> {"list":[],"dict":{}} 

You can also easily manipulate it (as it would with an associative array), and it will display correctly in the dictionary:

 $complex['dict']['a'] = 123; print json_encode($complex); // -> {"list":[],"dict":{"a":123}} unset($complex['dict']['a']); print json_encode($complex); // -> {"list":[],"dict":{}} 

If you want this to be 100% compatible in both directions , you can also wrap json_decode so that it returns ArrayObjects instead of stdClass (you will need to go through the result tree and recursively replace all objects, which is a pretty simple task).

Gotchas . Only the one I found so far: is_array(new ArrayObject()) evaluates to false . You may need to find and replace the is_array occurrences in your code (use (($foo instanceof ArrayObject) || is_array($foo)) ).

+48
May 21 '13 at 8:17
source share

Ok, json_encode() just returns a string from an array / PHP object / etc. You can achieve the same effect much more efficiently by doing:

 $json = '{}'; 

It makes no sense to use a function to accomplish this.

UPDATE According to your comments, you can try:

 $test = json_encode(array('some_properties'=>new stdClass)); 

Although I'm not sure what is better than what you did.

+14
Dec 21 '11 at 20:08
source share

To create an empty object in JSON with PHP, I used

 $json=json_decode('{}'); $json->status=202; $json->message='Accepted'; print_r($json); 

which produced

 stdClass Object ( [status] => 202 [message] => Accepted ) 

which is necessary because later I have to do it

 if(is_object($json)) 
+4
Aug 03 '14 at 12:16
source share



All Articles