Detect if REST API is running

I would use :

if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {...}

Source: https://github.com/WP-API/WP-API/issues/926#issuecomment-162920686

If you take a look to this source code on you will notice the constant is defined before processing anything, I would check for it instead of calling the mentioned function wp_is_json_request()

Why?
if headers were not set when you do the check, it will return false, here is a real example reported for a GET request: https://github.com/WordPress/gutenberg/issues/11327