refactored end() function and calling it from json()