When 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. The server will then provide links to get the previous and next JSON pages from the dataset. In this JSON pagination example, we limit the number of items in JSON to 5 and provide links to the previous and next JSON pages in the links JSON object.
What is JSON Pagination?
Pagination is the process of dividing a document into separate sequential pages that are related and have similar content. For JSON, pagination refers to displaying a little 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 the page 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 provide the server with the page number and page size, such as a custom HTTP headers.
API Pagination Example
Servers may have a default limit on the number of results in JSON. Still, 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 to 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 to the next page, you must send a request to the "next" URL. This will deliver a new JSON with new results and new links for the next and previous pages.