Close connection before return JSON