如何获取和比较Postgres中jsonb数组的元素?

How to get and compare the elements of the jsonb array in Postgres?

Postgres 9.6.1


    创建 TABLE "public"."test" (
    "id" int4 不为空,
    "packet" jsonb,
    )
    与 (OIDS=FALSE)
    ;

Jsonb


    {“1”:{"end":14876555,"quantity":10},“2”:{"end":14876555,"quantity":10}}


    [{"op": 1, "end": 14876555, "quantity": 10}, {"op": 2, "end": 14876555, "quantity": 20}]

所有检索数组的尝试都会导致错误: 无法从对象中提取元素

需要比较所有"end"<1490000的元素,找到id

"op": 1 or "1": 变量值和完整路径不适合解决方案

如果您没有约定的 JSON 结构,IMO 的最佳解决方案是

select *
from
  public.test,
  regexp_matches(packet::text, '"end":\s*(\d+)', 'g') as e(x)
where
  x[1]::numeric < 1490000;