Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONB column does not exist

I got this error.

ERROR: column "name" does not exist LINE 6:

SELECT JS, location, location->>Name

                              ^ SQL state: 42703 Character: 278

from this query

WITH JsonTable AS
(
    SELECT 
    '[
        {
            "Id":1,
            "Name":"A01",
            "Address":"aaa",
            "SearchVector":null,
            "ParentLocationId":null
        },
        {
            "Id":4,
            "Name":"B-01",
            "Address":"bbb",
            "SearchVector":null,
            "ParentLocationId":null
        }
    ]'::jsonb AS JS
) 
SELECT JS, location, location->>Name
FROM JsonTable, jsonb_array_elements(JS) x (location)

How can I select JSON value?

like image 535
Jongz Puangput Avatar asked Jul 21 '26 15:07

Jongz Puangput


1 Answers

You are missing quotes around the name of the JSON attribute that you want to select. Just like object keys must always be quoted when the JSON object is declared, they need to be quoted when they are accessed.

See the Postgres documentation for the JSON datatype and JSON Functions and Operators.

You would need to change this:

SELECT JS, location, location->>Name

To:

SELECT JS, location, location->>'Name'

Demo on DB Fiddle.

like image 64
GMB Avatar answered Jul 23 '26 08:07

GMB



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!