Fix memory leak and usage of json api