在Postgres中查询JSON时出错:函数json_extract_path_text(text,text)不存在

alx*_*lvt 2 postgresql json

我正在尝试基于对Postgres的最新版本(9.3.4)中包含JSON 的文本列的查询来建立视图,但是出现一个我无法找到任何讨论的错误。

假设该表名为table1,并且特定列json_data具有类似

{"item1": "value1", "item2": "value2", "item3": 3, "item4": 4, "item5": 5}
Run Code Online (Sandbox Code Playgroud)

这是我的查询:

SELECT 
json_extract_path_text((table1.json_data)::text, 
('item1'::character varying)::text) AS item1
FROM
table1
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

ERROR:  function json_extract_path_text(text, text) does not exist
LINE 2: json_extract_path_text((table1.json_data)...
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题。(此外,我有一个相似的视图,在特定表的相似文本列上使用相同的语法,可以完全正常工作。)

小智 6

由于table1.json_data已经是文本,因此您需要将其转换为json。您也不需要指定表名,因为它在FROM子句中。

SELECT json_extract_path_text(json_data::json,'item1') AS item1 FROM table1;
Run Code Online (Sandbox Code Playgroud)


Cra*_*ger 5

由于某种原因,您将json输入强制转换为text

json_extract_path_text((table1.json_data)::text
Run Code Online (Sandbox Code Playgroud)

不要那样做

SELECT 
    json_extract_path_text(
        table1.json_data,
        'item1'
    ) AS item1
FROM table1
Run Code Online (Sandbox Code Playgroud)