move the JSON generation out of the transaction