See also the description at:
https://eu.swi-prolog.org/pldoc/man?section=prolog-canonical-json
which is subtly different.
Do not use this with cyclic structures!
?- A=[1,2,3|A],term_to_json(A,X). ERROR: Stack limit (1.0Gb) exceeded
Also, I can't make the description correspond to what actually happens:
?- term_to_json(1,X). X = 1. ?- term_to_json(1.1,X). X = 1.1. ?- term_to_json(thisisanatom,X). X = thisisanatom. ?- term_to_json(somedict{a:1},X). X = somedict{a:1}. ?- term_to_json(A,X). X = "_". ?- term_to_json([1,2,3],X). X = [1, 2, 3]. ?- term_to_json(foo(bar,baz),X). X = json{args:[bar, baz], functor:foo}. ?- term_to_json(json([a=1,b=2]),X). false.