Returning a json array slice in PostgreSQL
select array_to_json
(
(select array_agg(my_db_field->n order by n)
from generate_series(0,least(300,json_array_length(my_db_field))-1) gs(n)
)
)
from my_db_table
This solution (the original in this answer) most likely does not guarantee the order of the elements
select (array(select json_array_elements(my_db_field)))[1:300]
from my_db_table
In PostgreSQL 12, you can obtain JSONB array slice using the jsonb_path_query_array
function:
SELECT jsonb_path_query_array('["a","b","c","d","e","f"]', '$[2 to 4]');
jsonb_path_query_array
------------------------
["c", "d", "e"]
(1 row)