Moving lines that write json stream to a file into its own function