Use the wp_send_json() function.