Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL query on a JSONB Array of Objects

I've a PostgreSQL table with this complex structure :

"item": [{
        "field10",
        "field11",
        "field12": {
            "field20"
        },
        "field13": [{
                "field21": [{
                        "field30"
                    }
                ],
                "field22": [{
                        "field31"
                        "field32": {
                            "field40"
                        }
                    }
                ],
            }
        ]
    }]

And I've to make a query for filter until the field40 many explication than I saw are for objects mor simple. Can you explain me how can I do ?

Edit :

The example what I made i more simplest than reality. In reality the object is very very long. If I try to make a simple example with values, that's like that :

{
"uuid": "64fc7a55-be2d-41d8-99c1-d08fa5780731",
"item": [{
            "field10" : 2004,
            "field11" : false,
            "field12": {
                "field20" : "cc092aa1-6465-4526-9292-373344ed3e18",
                "field21" : "AMAZONE"
            },
            "field13": [{
                    "field22": [{
                            "field30" : "ee5bfd21-b2ce-453e-94a2-ef9c87002758",
                            "field31" : "MICROSOFTE"
                        },{
                            "field30" : "2eb39939-08b9-4456-9b5b-eff88f00bacb",
                            "field31" : "GOOGLEE"
                        }
                    ],
                    "field23": [{
                            "field32" : "e70bec99-689a-4c5d-ab81-e7601bc5b435",
                            "field33": {
                                "field40" : "a70ce09e-21ec-46cd-bfe6-591b84c0b328",
                                "field41" : "https://stackoverflow.com/"
                            }
                        },{
                            "field32" : "e70bec99-689a-4c5d-ab81-e7601bc5b435",
                            "field33": {
                                "field40" : "7bfa6882-2e65-4f96-b4a5-6aa0d148a95d",
                                "field41" : "https://gis.stackexchange.com/"
                            }
                        }
                    ],
                }
            ]
        }]
},
{
"uuid": "f131864a-338e-4f2c-91b1-13950958bd62",
"item": [{
            "field10" : 2012,
            "field11" : false,
            "field12": {
                "field20" : "2b8176e4-c858-4159-b279-d852df77c63b",
                "field21" : "PIXABAYE"
            },
            "field13": [{
                    "field22": [{
                            "field30" : "9e70b3d8-8f3f-4ae1-ba64-86efa513bd28",
                            "field31" : "IOBEYAE"
                        },{
                            "field30" : "0150dba2-98de-4143-bd81-329b74874403",
                            "field31" : "PINTERESTE"
                        }
                    ],
                    "field23": [{
                            "field32" : "3ab19111-513a-47db-9fb5-52dc0a1e1c4d",
                            "field33": {
                                "field40" : "7be95726-6698-4bdd-8f6b-2595d1704a2a",
                                "field41" : "https://google.com/"
                            }
                        },{
                            "field32" : "7993aba5-f13f-4998-af7b-9b0b8aec8aa9",
                            "field33": {
                                "field40" : "4740dbcf-84c4-40ff-a633-9207e79cabf4",
                                "field41" : "https://yahoo.com/"
                            }
                        }
                    ],
                }
            ]
        }]
}

In this example, my Postgres table have two columns, one with the primary key uuid and one with the object in jsonb item And what I want, for example is to make a filter on my items where "field41" contains value "gis.stackexchange.com" for et in this example if I search "field41" = "https://yahoo.com/" the tuple with "uuid" = "uuid": "64fc7a55-be2d-41d8-99c1-d08fa5780731" will not be in the result

like image 868
Geo-x Avatar asked Apr 29 '26 14:04

Geo-x


1 Answers

With PostgreSQL v12 and better, you can use the jsonpath language to write such a query:

SELECT uuid
FROM jtab
WHERE jsonb_path_exists(
         item,
         '$[*].field13[*].field23[*].field33.field41 ? (@ == "https://yahoo.com/")'
      );
like image 198
Laurenz Albe Avatar answered May 01 '26 06:05

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!