无法在 Postgresql 中将列转换为 JSON(标记 "top" 无效)

Cannot convert column to JSON in Postgresql (Token "top" is invalid)

我想在 Postgresql 9.4.4 数据库中将其中一个列从 VARCHAR 更改为 JSON。该列包含文本以及 null 个值。

但是,我收到错误 Token "top" is invalid.,我根本不明白。

ALTER TABLE schema1.table1
  ALTER COLUMN col5 TYPE JSON USING (col5::JSON);

ERROR: invalid input syntax for type json
   Detail: Token "top" is invalid.
   Where: JSON data, line 1: top...

我怎样才能完成这项工作?

col5 包含无效值 json。您可以使用以下函数进行检查:

create or replace function valid_json(str varchar)
returns boolean language plpgsql as $$
declare
    j json;
begin
    j:= str;
    return true;
exception
    when others then return false;
end $$;

select *
from schema1.table1
where not valid_json(col5);