Pagination and API data handling
Pagination divides a collection into smaller responses. A client follows a documented cursor, page number or next link until the documented termination signal. A missing cursor is not automatically a valid end; a server may return a malformed or incomplete page. A short page is not proof of completion unless the API specifies it.
Bound the iteration
The lab uses a fixed contract: one object with an items array of short string IDs and a next value that is either null (end) or an allowed cursor. Use a page budget, detect repeated cursors, validate response structure and check the fetch status before extracting values. A cursor is opaque data in a real API; use URL encoding for query values. Do not blindly follow arbitrary URLs from responses, especially with credentials attached. Keep origins fixed or validate them under a reviewed policy.
Stage all page results privately before publishing; an error after a few pages otherwise leaves a misleading partial report. The first example uses two trusted files representing pages; it stages a text report and prints only after the last validated page. The cursor is explicitly restricted to 2 in this tiny contract. The second demonstrates cycle detection in a mock sequence, and the third validates a malformed page. For real APIs, also enforce response-size and item limits, handle consistency or changing snapshots, deduplicate only according to a documented identity policy, and distinguish missing, null and empty fields. Never parse arbitrary JSON with shell word splitting. The first collector now rejects revisiting an already seen cursor before reading the next file. It still uses a finite page budget. Its output is staged in a fresh private lab directory; a failed write or invalid page must prevent publication.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. HTTP examples require the included lab-fixtures/v25/mock_api.py server: run python3 on that file in a separate terminal, export BM_API_BASE with its printed loopback URL, and stop the server with Ctrl-C when done. Use a fresh server for each HTTP example. curl and jq are external dependencies.
Collect two validated pages
umask 077printf '%s\n' '{"items":["a","b"],"next":"2"}' > page1.json || exit 1printf '%s\n' '{"items":["c"],"next":null}' > page2.json || exit 1declare -A seen=()cursor=1: > staged.txt || exit 1for ((page=1; page<=3; page++)); do case $cursor in 1|2) ;; *) exit 2;; esac [[ ! ${seen[$cursor]+yes} ]] || exit 2 seen[$cursor]=1 file="page$cursor.json" jq -es 'length==1 and (.[0] | type=="object" and has("next") and (.next==null or .next=="2") and (.items|type=="array") and all(.items[]; type=="string" and test("\\A[a-z]{1,8}\\z")))' "$file" >/dev/null || exit 2 jq -r '.items[]' "$file" >> staged.txt || exit 2 cursor=$(jq -r '.next' "$file") || exit 2 if [[ $cursor == null ]]; then cat staged.txt; exit $?; fidoneexit 2Expected stdout:
abcThe page budget prevents endless iteration; the fixed cursor set prevents arbitrary paths. Only a validated terminal page permits printing.
Detect a repeated cursor
declare -A seen=()for cursor in A B A; do if [[ ${seen[$cursor]+yes} ]]; then printf 'cycle=%s\n' "$cursor" break fi seen[$cursor]=1 printf 'visit=%s\n' "$cursor"doneExpected stdout:
visit=Avisit=Bcycle=AThe fixture uses fixed trusted cursor names. The demonstration reports the cycle; a real client must stop and mark failure.
Reject missing termination field
printf '%s\n' '{"items":[]}' > page.json || exit 1if jq -e 'has("next") and (.next == null or (.next|type=="string"))' page.json >/dev/null; then printf 'valid\n'else rc=$? [[ $rc == 1 ]] || exit 1 printf 'invalid page\n'fiExpected stdout:
invalid pageAn empty list does not repair a missing required cursor field.
Common mistakes
Do not treat a missing next field as null; do not publish partial results as complete. A page budget is needed even if cycle detection exists.
Practice
Change page 2 to refer back to a seen cursor, omit next, or supply invalid item data. Predict why publication is blocked; keep all requests synthetic.
Next: Retries, backoff and idempotency