Pretty printing a json term

Finally found that library(http/json) has what I need. The width option controls the thing. I propose that the portray_clause/1 should pretty print json terms using this json_write/3

 json_write(current_output, json([name='Bob', children=['Mary', 'John'], age=42, married= @(true)]),[width(8)]).

Result is

{
  "name":"Bob",
  "children": [
    "Mary",
    "John"
  ],
  "age":42,
  "married":true
}

No. portray_clause/1 is defined to print the term such that it is valid syntax and reads back to the original. You do get acceptable results if you use the β€œdict” variation of the JSON API.

1 Like

Thanks.

What I found is to assert a portray/1

?- [library(http/json)].
true.

?-  asserta(portray(json(X)):- (json_write(current_output, json(X),[width(8)]))).
true.

?- print( json([name='Bob', children=['Mary', 'John'], age=42, married= @(true)])).
{
  "name":"Bob",
  "children": [
    "Mary",
    "John"
  ],
  "age":42,
  "married":true
}
true.

Also format(β€˜~p’ …) prints the json term nicely.

I am happy with this.