I am sure that examples exist in the wild of attaching an audit trigger to a table so that the json form of the OLD and NEW (the PostgreSQL version of "Inserted" and "Deleted" (more or less?) records can be either directly inserted into another table or passed to a function that handles that task (simple functional encapsulation).
In particular you should review the documentation:
Both the "record" oriented one and the newer "transition tables" one - which is probably more similar to what your example code does (haven't used that feature myself).