refactoring of JSON formatting logic