如何在指定路径- postgresql获取JSON对象

rsaldnfx  于 2023-01-08  发布在  PostgreSQL
关注(0)|答案(1)|浏览(132)

我尝试在JSON对象中的指定路径获取item_sku元素。
我有一个列,我只想使用postgresql提取item_sku添加到一个列中。我该怎么做才能解决这个问题?
下面是我正在尝试的代码:

select o.reference,
       o.id as "ord_id",
       o.created_at,
       o.aasm_state,
       o.payment_details -> 'payment_method' as "payment_method", 
       max(gr.updated_at) as "last_updated_at", 
       o.shipping_address -> 'country' as "country",
       (gr.request_body -> 0 #>> '{lines}') as ESD

这里的结果我得到:

[{"item_sku": "60997070103", "image_url": "https://hahaha", "identifier": "7713128"}, {"item_sku": "60997070103", "image_url": "https://hahha", "identifier": "7713131"}, {"item_sku": "62007100402", "image_url": "https://hahha", "identifier": "7713129"}]

以下是我试图获得的信息的数据:

[{"lines": [{"item_sku": "60997070103", "image_url": "https://hahaha", "identifier": "7713128"}, {"item_sku": "60997070103", "image_url": "https://hahha", "identifier": "7713131"}, {"item_sku": "62007100402", "image_url": "https://hahha", "identifier": "7713129"}]
ttvkxqim

ttvkxqim1#

SELECT gr.request_body #>> '{0,lines,0,item_sku}'将产生"60997070103"
SELECT gr.request_body #>> '{0,lines,1,item_sku}'将产生"60997070103"
SELECT gr.request_body #>> '{0,lines,2,item_sku}'将产生"62007100402"

SELECT elt->>'item_sku' AS item_sku
  FROM jsonb_array_elements(gr.request_body#>'{0,lines}') AS elt

将导致
| 项目_SKU|
| - ------|
| 小行星60|
| 小行星60|
| 小行星62007|
参见dbfiddle

相关问题