Dev Helpers

SQL / Grouping

Slice a JSON array into named pages

Dev Helpers · December 25, 2025

Integer array indexes can be divided into page buckets. Sort the elements within each aggregate to preserve the source sequence.

Worked example

SELECT key/2+1 AS page,json_group_array(value ORDER BY key) AS items FROM json_each('["a","b","c","d","e"]') GROUP BY page ORDER BY page;

Result

[
  {
    "page": 1,
    "items": "[\"a\",\"b\"]"
  },
  {
    "page": 2,
    "items": "[\"c\",\"d\"]"
  },
  {
    "page": 3,
    "items": "[\"e\"]"
  }
]

Implementation notes

Page labels in the example are one-based while JSON indexes are zero-based. Keep that conversion at the display boundary.

Try it yourself

Use an odd number of items and inspect the shorter last page.

Open the SQL example →

Editorial date December 25, 2025 · Published December 6, 2026

Related articles

Compare two literal lists

Set operations compare rows under SQL's rules for duplicates. Check whether the result represents membership or the number of occurrences.

Count jobs per active day

Grouping timestamps by day produces counts only for days present in the data. A continuous chart may need a calendar series as well.