1

I am trying to perform an update with a cursor variable in a previous step, but in a jsonb field, the system returns an error. Contend field is a jsonb type. I am doing it as follows:

¿Can you help me please?

Update OPTION 1

update test
set
content = jsonb_set(content, '{supplier}', '' || supplierId || ''::jsonb)
where operation_type = 'DEL';   

Error OPTION 1

SQL Error [22P02]: ERROR: invalid input syntax for type json Detail: The input string ended unexpectedly. Where: JSON data, line 1: PL/pgSQL function inline_code_block line 72 at SQL statement

Update OPTION 2

update test
set
content = jsonb_set(content, '{supplier}', '"' || supplierId || '"'::jsonb)
where operation_type = 'DEL';   

Error OPTION 2

SQL Error [22P02]: ERROR: invalid input syntax for type json Detail: Token """ is invalid. Where: JSON data, line 1: " PL/pgSQL function inline_code_block line 72 at SQL statement

2 Answers 2

2

First, cast the integer column to text and then to jsonb

jsonb_set(content, '{supplier}',  
          supplierId  :: text :: jsonb)

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you very much for your help. For Strings works perfectly, but numeric values show an error message.

content = jsonb_set(content, '{supplier}', supplierId::numeric::jsonb)

Error: SQL Error [42846]: ERROR: cannot cast type numeric to jsonb Where: PL/pgSQL function inline_code_block line 65 at SQL statement

Is there another way to convert it?

Example data:

'{"supplier": 1,"name": "Josh", "supplierId":10}'

Thank you very much,

EDIT: I resolved with: content = jsonb_set(content, '{supplier}', supplierId::integer::text::jsonb)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.