Fix term_to_json bugs