Handle errors when creating JSON object