Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Pagination in Calling JSON API, using APEX_JSON

Zack H.Jan 16 2025

I am trying to call the Microsoft Graph API, I am able to call the API, but I can only call 999 “rows” using $top. Here is my code:

I try to check for pagination using the following, is there another way to do this?

    -- Check for [@odata](https://forums.oracle.com/ords/apexds/user/odata).nextLink for pagination  
   begin  
       l\_next\_link := apex\_json.get\_varchar2 ('@odata.nextLink');  
   exception  
       when no\_data\_found then  
           l\_next\_link := null;  
   end;
function get_users_as_json return clob is
   l_request_url   varchar2 (4000) := 'https://graph.microsoft.com/v1.0/users?$top=999&';
   l_access_token  varchar2 (4000);
   l_clob_response clob;
   l_next_link     varchar2 (4000);
   l_first_page    boolean := true;
begin

   -- Initialize the JSON output
   apex_json.initialize_clob_output;
   apex_json.open_object;
   apex_json.open_array ('value');

   -- Get the access token
   l_access_token  := web_api_pkg.get_access_token ('Microsoft Graph');
   loop

       -- Set HTTP headers
       apex_web_service.set_request_headers (
           p_name_01  => 'Authorization',
           p_value_01 => 'Bearer ' || l_access_token,
           p_name_02  => 'Accept',
           p_value_02 => 'application/json'
       );

       -- Make the API request
       l_clob_response := apex_web_service.make_rest_request (
           p_url         => l_request_url,
           p_http_method => 'GET'
       );

       -- Parse and process the current page's JSON response
       apex_json.parse (
           p_source => l_clob_response
       );
       for i in 1..apex_json.get_count ('value') loop
           apex_json.open_object;
           apex_json.write ('id', apex_json.get_varchar2 ('value[' || (i - 1) || '].id') );
           apex_json.write ('accountEnabled', apex_json.get_varchar2 ('value[' || (i - 1) || '].accountEnabled') );
           apex_json.write ('displayName', apex_json.get_varchar2 ('value[' || (i - 1) || '].displayName') );
           apex_json.write ('givenName', apex_json.get_varchar2 ('value[' || (i - 1) || '].givenName') );
           apex_json.write ('surname', apex_json.get_varchar2 ('value[' || (i - 1) || '].surname') );
           apex_json.write ('mail', apex_json.get_varchar2 ('value[' || (i - 1) || '].mail') );
           apex_json.write ('mailNickname', apex_json.get_varchar2 ('value[' || (i - 1) || '].mailNickname') );
           apex_json.write ('jobTitle', apex_json.get_varchar2 ('value[' || (i - 1) || '].jobTitle') );
           apex_json.write ('department', apex_json.get_varchar2 ('value[' || (i - 1) || '].department') );
           apex_json.close_object;
       end loop;

       -- Check for @odata.nextLink for pagination
       begin
           l_next_link := apex_json.get_varchar2 ('@odata.nextLink');
       exception
           when no_data_found then
               l_next_link := null;
       end;

       -- Exit the loop if no next page exists
       exit when l_next_link is null;

       -- Update the request URL for the next page
       l_request_url := l_next_link;
   end loop;

   -- Close the JSON structure
   apex_json.close_array;
   apex_json.close_object;

   -- Retrieve the final JSON output
   l_clob_response := apex_json.get_clob_output;

   -- Insert the JSON into the database table
   insert into users_json_responses (
       response
   ) values (
       l_clob_response
   );
   commit;

   -- Return the JSON
   return l_clob_response;
exception
   when others then
       dbms_output.put_line ('Error: ' || sqlerrm);
       apex_json.free_output;
       rollback;
       return null;
end;
Comments
Post Details
Added on Jan 16 2025
4 comments
740 views