简体   繁体   English

Postgres中的jsonb查询

[英]jsonb query in postgres

I've a table in postgres named: op_user_event_data, which has a column named data, where I store a jsonb, and what I have at the moment is a json like this: 我在postgres中有一个名为op_user_event_data的表,其中有一个名为data的列,我在其中存储jsonb,此刻我拥有的是这样的json:

{
    "aisles": [],
    "taskGroups": [
        {
            "index": 0,
            "tasks": [
                {
                    "index": 1,
                    "mandatory": false,
                    "name": "Dados de Linear",
                    "structuresType": null,
                    "lines": [
                        {
                            "sku": {
                                "skuId": 1,
                                "skuName": "Limiano Bola",
                                "marketId": [
                                    1,
                                    3,
                                    10,
                                    17
                                ],
                                "productId": 15,
                                "brandId": [
                                    38,
                                    44
                                ]
                            },
                            "taskLineFields": [
                                {
                                    "tcv": {
                                        "value": "2126474"
                                    },
                                    "columnType": "skuLocalCode",
                                    "columnId": 99
                                },
                                {
                                    "tcv": {
                                        "value": null
                                    },
                                    "columnType": "face",
                                    "columnId": 29
                                },
                            ]
                        },
                        {
                            "sku": {
                                "skuId": 3,
                                "skuName": "Limiano Bolinha",
                                "marketId": [
                                    1,
                                    3,
                                    10,
                                    17
                                ],
                                "productId": 15,
                                "brandId": [
                                    38,
                                    44
                                ]
                            },
                            "taskLineFields": [
                                {
                                    "tcv": {
                                        "value": "2545842"
                                    },
                                    "columnType": "skuLocalCode",
                                    "columnId": 99
                                },
                                {
                                    "tcv": {
                                        "value": null
                                    },
                                    "columnType": "face",
                                    "columnId": 29
                                },
                            ]
                        },
                        {
                            "sku": {
                                "skuId": 5,
                                "skuName": "Limiano Bola 1/2",
                                "marketId": [
                                    1,
                                    3,
                                    10,
                                    17
                                ],
                                "productId": 15,
                                "brandId": [
                                    38,
                                    44
                                ]
                            },
                            "taskLineFields": [
                                {
                                    "tcv": {
                                        "value": "5127450"
                                    },
                                    "columnType": "skuLocalCode",
                                    "columnId": 99
                                },
                                {
                                    "tcv": {
                                        "value": "5.89"
                                    },
                                    "columnType": "rsp",
                                    "columnId": 33
                                }
                            ]
                        }

Basically I've an object which has Aisles [], taskGroups, id and name. 基本上,我有一个具有走道[],taskGroups,ID和名称的对象。

Inside the taskGroups as shown in the json, one of the atributes is tasks which is an array, that also have an array called lines which have an array of sku and tasklines. 在json中所示的taskGroups内部,属性之一是任务,它是一个数组,也有一个名为lines的数组,其中有sku和tasklines数组。

Basically: 基本上:

taskGroups -> tasks -> lines -> sku or taskLineFields. taskGroups-> task-> lines-> sku或taskLineFields。

I've tried different queries to get the sku but when I try to get anything further than 'lines' it just came as blank or in some other tries 'cannot call elements from scalar' 我尝试了不同的查询来获取sku,但是当我尝试获取除“行”以外的任何内容时,它只是空白或在其他尝试中“无法从标量调用元素”

Can anyone help me with this issue? 谁能帮我解决这个问题? Note this is just a sample json. 请注意,这只是一个示例json。

Anyone knows how make this to work: 任何人都知道如何使它起作用:

I Want all lines where lines->taskLineFields->columnType = 'offer' 我想要所有行-> taskLineFields-> columnType ='offer'的行

All I can do is this, but throwing error on scalar: 我所能做的就是这样,但是在标量上抛出错误:

    SELECT lines->'sku' Produto, lines->'taskLineFields'->'tcv'->>'value' ValorOferta
FROM sd_bel.op_user_event_data,
     jsonb_array_elements(data->'taskGroups') taskgroups,
     jsonb_array_elements(taskgroups->'tasks') tasks,
     jsonb_array_elements(tasks->'columns') columns,
     jsonb_array_elements(tasks->'lines') lines
WHERE created_by = 'belteste'
AND  lines->'taskLineFields'->>'columnType' = 'offer'

say your data is in some json_column in your table 说你的数据在table json_column

with t as (
    select json_column as xyz from table
),
tg as ( select json_array_elements(xyz->'taskGroups') taskgroups from t),
tsk as (select json_array_elements(taskgroups->'tasks') tasks from tg)
select json_array_elements(tasks->'lines') -> 'sku' as sku from tsk;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM