简体   繁体   中英

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:

{
    "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.

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.

Basically:

taskGroups -> tasks -> lines -> sku or 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'

Can anyone help me with this issue? Note this is just a sample json.

Anyone knows how make this to work:

I Want all lines where lines->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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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