JSON Menu Link Without Invoking Init Hooks? Our Drupal site utilizes a number of menu hooks to return JSON from an API we manage in a separate Web API. We also have a number of intensive tasks that happen on each page load via the hooks_init in several custom modules.

The issue we're facing is that the tasks in hook_init are only required for actual pages, and should not be executed when all we want is JSON output. Is there a way to ONLY execute the hook_menu endpoint, and no other Drupal code?

We have several pages that make 3-4 requests to Drupal JSON menu links, which means that we also run our unnecessary hook_init tasks 3-4 times as well -- plus once for the actual page request.

There must be a way to circumvent the usual Drupal pipeline to perform these actions?

Here is an example of one of our JSON menu hooks:

function membership_details()
{

    // Get membership details from API, and return as JSON
    $details =  sendCurlResponse(/* ENDPOINT */);
    drupal_json_output($details);
    drupal_exit();
}
hook_init() is invoked before the menu system deals with the request, and in the confines of a normal Drupal bootstrap, no, it's not possible to circumvent that workflow.

It's by design though, if you look at the docs:


  Perform setup tasks for non-cached page requests.
  
  This hook is run at the beginning of the page request. It is typically used to set up global parameters that are needed later in the request. When this hook is called, the theme and all modules are already loaded in memory.


Emphasis mine.

So for its intended use, hook_init should only set up variables that are required for the current request. Whether that be a request for HTML, JSON, or whatever else. 

You have two pretty reasonable options as far as I can tell:


Augment the code you have in hook_init implementations to check whether it should be executed based on parameters from the request (accepted content type, file extension, whatever makes the most sense for you use case).
Move the logic in hook_init to another hook, one that's invoked later in the process (I can't really say which hook that would be without knowing what the code does)