If the number of items returned in the server's JSON response is too large, the server can limit the number of items in the JSON to a small subset ("page") of the total available set to reduce the amount of data transferred from the server and speed up the server response time. In this case, the server will provide links to get the previous and next JSON pages from the dataset. In this JSON pagination example, we limit the number of resources in JSON to 3 and provide links to the previous and next JSON pages in the links JSON object. The PHP code was automatically generated for the JSON Pagination example.
What is JSON Pagination?
Pagination is an ordinal numbering, indicated by columns located at the bottom, top, or side. For JSON, pagination refers to displaying a limited chunk of data for a large dataset (for example, the first 100 results from an API response containing 1000 items). Pagination is commonly used in web applications to paginate large amounts of data and usually includes a navigation box for navigating to other pages.
The page number is usually passed as a URL parameter along with a size parameter (to limit the number of items in the response) in the API request. If no page number is passed, the default is the first page. Some API providers may use other ways to pass the page number and data limit to the server, for example, as a custom HTTP header.
API Pagination Example
Servers may have a default limit on the number of results in JSON, but instead of relying on this limit, it is recommended that you explicitly set the limit parameter in the API request to know exactly how many results will be in the JSON response.
How do I get the previous and next pages when paginating the JSON?
Paginated JSON will usually have an object with links to the previous and next JSON pages. To get the previous page, you must send a request to the "prev" URL, to get the next page, you must send a request to the "next" URL. This will deliver a new JSON with a set of new results and new links for the next and previous pages.