错误:函数json_typeof(json)不存在

时间:2018-12-19 09:44:07

标签: sql postgresql postgresql-9.3

在Postgres版本9.3.9中运行函数时遇到问题,运行此函数时出现以下错误:

Caused by: org.postgresql.util.PSQLException: ERROR: function json_typeof(json) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Where: PL/pgSQL function myArray(character varying,json) line 9 at SQL statement

这是我的功能:

CREATE OR REPLACE FUNCTION myArray(domain_name varchar, val json) RETURNS varchar LANGUAGE plpgsql STABLE AS $$
DECLARE
  result varchar;
  isarray BOOLEAN;
    q cursor for
    select json_agg(blogIn(null,b.value))
    from json_array_elements_text(val) b;
BEGIN
  SELECT json_typeof(val) = 'array' into isarray;
  if not isarray THEN
    return val;
  end if;
  open q;
  fetch q into result;
  close q;
  if result is null then
    return val;
  end if;
  return result;
END;
$$;

此功能在Postgres 9.5版本中运行没有问题,但在9.3上会产生上述错误,这很奇怪。有人可以告诉我在“ SELECT json_typeof(val)='array'into isarray;”中实际进行类型强制转换的要求吗? ?

1 个答案:

答案 0 :(得分:1)

当然,您很快就会升级Postgres。在此之前,您可以使用以下功能:

create or replace function is_json_array(json)
returns boolean language sql immutable as $$
    select coalesce(left(regexp_replace($1::text, '\s', '', 'g'), 1) = '[', false)
$$;
相关问题