Catch errors in outputting JSON