Fix memory leak with error reporting in json format