WordPress Shortcode / Only works in classic editor, gutenberg error / invalid json response

A shortcode should only ever return content, never echo it. The bits between the ?> and <?php tags — the <script> tags and the rest — are effectively being echoed to the browser, which means that the JSON content that Gutenberg expects is no longer valid.

From the add_shortcode() docs:

Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.

Also, instead of sending those <script> tags directly, you should enqueue them. See wp_enqueue_script(). Since you are passing data from PHP to at least one of your scripts, see also wp_localize_script().