Rebase #6094 "Provide a more useful definition of json_escape" and update docs.

See #6094 for discussion.