refactored to-json function, added tests