refactoring additional logic for formatting JSON