Check for nil before attempting to serialize JSON data.