Moved to_json() to utils, added a test and fixed a bug