Separate JSON formatting in a function