I think you should stop using rewrite endpoints to handle JSON responses. Instead you can and you should use the REST API.
So, instead of this:
function makeplugins_add_json_endpoint() {
add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
}
add_action( 'init', 'makeplugins_add_json_endpoint' );
And then handle the JSON response at your own, you can and you should do something like this:
add_action( 'rest_api_init', 'cyb_register_api_endpoints' );
function cyb_register_api_endpoints() {
$namespace="myplugin/v1";
register_rest_route( $namespace, '/myendpoint/', array(
'methods' => 'GET',
'callback' => 'cyb_myendpoint_callback',
) );
}
function cyb_myendpoint_callback() {
//$response can be a Object, JSON, array
$response = [];
return new WP_REST_Response( $response );
}
In conclusion: a JSON endpoint should be handle now through REST API.