0

I am trying to get the opportunity equals resales from the following JSONB array in Postgres. But I can't seems to figure it out.

{
    "done": true,
    "size": 106,
    "records": [{
        "Name": "FEF",
        "IsActive": true,
        "attributes": {
            "price": "3",
            "width": "20"
        },
        "Description": null,
        "Opportunity": "Resale"
    }, {
        "Name": "DHQ",
        "IsActive": true,
        "attributes": {
            "price": "300",
            "width": "10000"
        },
        "Description": null,
        "Opportunity": "Resale"
    }]
}
SELECT  salesdata 
FROM public.salesdata 
where salesdata -> 0 ->> '"records":[{"Opportunity":"Resale"}]';

Error:

SQL Error [42804]: ERROR: argument of WHERE must be type boolean, not type text

2
  • i am using version 12 of postgre
    – lancegoh
    Commented Dec 31, 2019 at 11:35
  • What is the output you expect?
    – user330315
    Commented Dec 31, 2019 at 11:36

1 Answer 1

2

The ->> operator returns the content of the element specified. If you want to test for the presence of a value, use the contains operator @>

Also the top level JSON you have isn't an array, so salesdata -> 0 doesn't make sense.

To test if the array identified by the key records contains at least one key/value pair with specific value, you can use:

SELECT salesdata 
FROM public.salesdata 
where salesdata -> 'records' @> '[{"Opportunity":"Resale"}]';

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.