函数json_each不存在

Bil*_*lla 4 postgresql json

我收到json_each函数不存在的错误。我正在使用PostgreSQL 9.3。我不知道怎么了。请在这里帮助我。

select *
from json_each((
    select ed.result
    from externaldata ed
    inner join 
    application a
    on a.id = ed.application_id

))
limit 1;
Run Code Online (Sandbox Code Playgroud)

内部循环查询返回:

" { "RespuestaSVC89":{
    "Header":{
      "Transaccion":"EXPE",
      "Servicio":"92",
      "CodigoRetorno":"00",
      "NumeroOperacion":"201409147001616",
      "CodigoModelo":"13852901"
    },
    "meta":{
      "billa":"EXPE",
      "numo":"52",
      "Retorno":"01",
      "Operacion":"2014091470",
    }
   }
}"
Run Code Online (Sandbox Code Playgroud)

所以它应该可以工作,但是不起作用

确切的错误消息是:

ERROR:  function json_each(text) does not exist
LINE 2: from json_each((
             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********

ERROR: function json_each(text) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 15
Run Code Online (Sandbox Code Playgroud)

Joe*_*ove 6

错误消息指出不存在json_each(text)函数,但是我知道存在json_each(json)函数。关键是将ed.result转换为json数据类型,如下所示:

select *
from json_each((
    select ed.result::json
    from externaldata ed
    inner join 
    application a
    on a.id = ed.application_id

))
limit 1;
Run Code Online (Sandbox Code Playgroud)

如果您的数据确实是有效的json,则可以考虑使ed.result列的类型为json(在实际表中)而不是文本类型。当9.4发布时,您几乎肯定会想要使用jsonb数据类型来利用该数据类型带来的性能和空间优势。


Tor*_*ino 5

这可能是另一个可能的原因:

包含 json 的列的类型不是 类型json,而是jsonb.

在这种情况下,您不应该使用json_eachfunction,而应该使用jsonb_each.

例子:

create table metric
(
    user_id    bigint                   not null,
    created_at timestamp with time zone not null,
    data       jsonb                    not null,
    constraint metric_pk
        primary key (user_id, created_at)
);
Run Code Online (Sandbox Code Playgroud)

询问:

select metric.created_at, jsb.key, jsb.value
from metric,
     json_each(data) as jsb
where user_id = <user_id>;
Run Code Online (Sandbox Code Playgroud)

结果是:

[42883] 错误:函数 json_each(jsonb) 不存在

没有函数与给定名称和参数类型匹配。您可能需要添加显式类型转换。

询问

select metric.created_at, jsb.key, jsb.value
from metric,
     jsonb_each(data) as jsb
where user_id = <user_id>;
Run Code Online (Sandbox Code Playgroud)

导致正确的结果: 在此输入图像描述