Search code examples
stringpostgresqlstring-concatenation

Concat in postgres pgjwt resulting in error


I am trying to use the pgjwt function but I get strange results when I concat text. Can someone explain what is going on here.

select sign ('"{foo": "bar"}', 'secret', 'HS512'); -- Works fine
select sign ('"{foo": "bar"}', 'secret', 'HS' || '512'); -- Works fine
select sign ('"{foo": "bar"}', 'sec' || 'ret', 'HS512'); -- Works fine
select sign ('"{fo' || 'o": "bar"}', 'secret', 'HS512'); -- Error
select sign ('"{foo":' || ' "bar"}', 'secret', 'HS512'); -- Error
select sign ('"{foo": "b' || 'ar"}', 'secret', 'HS512'); -- Error
select sign ('{}', 'sec' || 'ret', 'HS512'); -- Works fine
select sign ('{' || '}', 'sec' || 'ret', 'HS512'); -- Error

The error I get is:

ERROR: function sign (text, unknown, unknown) does not exist

Solution

  • You are splitting the JSON object into two strings of type text using the concatenation.

    A cast afterwards should do the job, I believe:

    select sign (('"{foo":' || ' "bar"}')::json, 'secret', 'HS512');