JSONB 列不存在

JSONB column does not exist

我遇到了这个错误。

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

SELECT JS, location, location->>Name

                              ^ SQL state: 42703 Character: 278

来自这个查询

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)

我怎样才能select JSON值?

您想要 select 的 JSON 属性名称周围缺少引号。就像声明 JSON 对象时必须始终引用对象键一样,访问它时也需要引用它们。

请参阅 the JSON datatype and JSON Functions and Operators 的 Postgres 文档。

你需要改变这个:

SELECT JS, location, location->>Name

收件人:

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

Demo on DB Fiddle.