refactored some code in json controller