我有一个存储在我的POSTGRES表中的JSON文档的结构
link to the sample JSON is here
这里是JSON,我在嵌套数组中有下面的结构,
"product_order_reference": {
"purchase_order_number": "0007-8653547-0590"
}
我正在尝试检索JSON,其中包含所提供的采购订单编号,我尝试了以下查询,即使它们是该采购订单编号的JSON行,查询也没有返回
我试过的查询:
SELECT * from edi_records , jsonb_array_elements(valid_record :: jsonb ->'loop_id_hls') hls,jsonb_array_elements(hls->'loop_id_hlo') hlo where hlo->'product_order_reference' ->> 'purchase_order_number' = '0007-8653547-0590';
SELECT * from edi_records , jsonb_array_elements(valid_record :: jsonb ->'loop_id_hls') hls,jsonb_array_elements(hls->'loop_id_hlo') hlo where hlo ->> 'purchase_order_number' = '0007-8653547-0590';
SELECT * from edi_records , jsonb_array_elements(valid_record :: jsonb ->'advance_shipment_notice'::text->'loop_id_hls') hls,jsonb_array_elements(hls->'loop_id_hlo') hlo where hlo ->> 'purchase_order_number' = '0007-8653547-0590';
SELECT track_num from edi_records , jsonb_array_elements(valid_record :: jsonb ->'advance_shipment_notice'->'loop_id_hls') hls,jsonb_array_elements(hls->'loop_id_hlo') hlo where hlo -> 'product_order_reference'->> 'purchase_order_number' ::text = '0007-8653547-0590';
任何人都可以帮我解决这个问题,我被困在这里。
答案 0 :(得分:1)
我复制并粘贴了你的JSON对象。它有点大,但我能得到订单号。主要的麻烦是所有嵌套数组。
缺点是我正在手动挖掘json对象。如果结构发生变化或者如果键包含需要稍微搜索的重复对象,则结果将是错误的。我相信这可以改进。
SELECT
your_json -> 'advance_shipment_notice'
-> 'loop_id_hls'
-> 0 -- {loop_id_hls}
-> 'loop_id_hlo'
-> 0 -- {loop_id_hlo}
-> 'product_order_reference'
-> 'purchase_order_number' AS purchase_order_number
FROM your_json;