T
T
The_Pro_Killer2021-11-25 09:49:48
Java
The_Pro_Killer, 2021-11-25 09:49:48

How to change value in JSONB?

I have a table that has a data field that is in JSONB format. This field has the following structure:
{"requisites":
{"kpp": "212997417444",
"ogrn":
{"number": "219322571340"}}
I need to change the value for the kpp field. I'm trying to do it like this:
update 'table name' set data = jsonb_set(data, '{requisites}', '{"kpp": 777}') where 'something';
But at the same time, everything else (for example, ogrn) is simply erased, and I need to save it and change only kpp. Tell me, please, what am I doing wrong? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-11-25
@The_Pro_Killer

set data = jsonb_set(data, '{requisites,kpp}', '777')

A
Akina, 2021-11-25
@Akina

You must specify the full path.

select jsonb_value,
       jsonb_set(jsonb_value, '{requisites,kpp}', '"777"', false)
FROM test;

https://dbfiddle.uk/?rdbms=postgres_12&fiddle=3cfb...
PS. In general, changing a string attribute to a numeric one is not safe. In the application, this may come back to haunt - it is not in the know ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question