Moved JSON encoding to helper function because DRY.