Skip to content
Advertisement

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?

Advertisement

Answer

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

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement