split JSON handling out into its own function