fixed toJson() function