massive json writing refactor