How to encode a nested empty array as an object

I am using CJavaScript::jsonEncode($data) to convert a nested object into json data.

One of the nested objects is normally an associative array and so it converts into a JSON object, however if it is empty it converts into a JSON array. How can I force it to convert to an object in all cases?

Solved it:

If the array is empty then redefine it as

$top_level_object[‘empty_object’] = new stdClass();

This causes the json encoder to treat it is an empty object.

More info: