Refactored the code with json