使用 json ot jsonb 查询获取标签

get tag with json ot jsonb query

我正在使用 PostgreSQL 11。 我正在尝试从这个 json 中获取 "wmnote" 标签(这是一个片段,有必要关闭标签): {

"order": [
    {
        "notes": {
            "note": []
        },
        "onHold": "false",
        "wmnotes": {
            "wmnote": []
        },
        "invoices": {
            "invoiceDetail": []
        },
        "confirmed": "true",
        "enteredBy": "",
        "entryType": "",
        "orderType": "DTC",
        "orderEvent": "Update",
        "orderLines": {
            "orderLine": [
                {
                    "notes": {
                        "note": []
                    },
                    "isGift": "false",
                    "itemID": "4027956",
                    "onHold": "false",
                    "wmnotes": {
                        "wmnote": [
                            {
                                "noteSeq": "1",
                                "noteCode": "",
                                "noteType": "DDate",
                                "visibility": "0",
                                "commentText": "02/07/2019"
                            }

这是我的查询:

select o.info->>'order'-> 'orderLines'->'wmnotes'->'wmnote'
            from customer_orders o
            where o.order_id = 1;

但结果为空。

列名信息是一种数据类型jsonb.

他们可以帮助我构建查询!!

三分:

  1. ->> 给出的文本不是类型 JSON。因此,您将无法将结果作为 JSON 对象进行处理。使用 -> 代替所有其他步骤
  2. order 包含一个数组。因此,您必须指定要搜索的数组元素。如果您想搜索第一个元素,您需要调用 "order" -> 0 -> "orderlines"(注意 JSON 数组是从零开始的!)
  3. orderline 也包含一个数组。见第 2 点。

因此您的查询应如下所示:

SELECT o.info->'order'->0 -> 'orderLines' -> 'orderLine' -> 0 -> 'wmnotes'->'wmnote'
FROM customer_orders o

demo: db<>fiddle