0

I have a numeric column total_price and I want to move it to a jsonb property named price_detail. For example, if the total_price value is 1000, the expected price_detail value is {"totalPrice": 1000}

I've tried using jsonb_set but the result is empty object {}

UPDATE public.orders SET price_detail =
       jsonb_set('{}'
               , '{}'
               , jsonb_build_object('totalPrice', total_price::numeric)) 

If I set the path,

UPDATE public.orders SET price_detail =
       jsonb_set('{}'
               , '{totalPrice}'
               , jsonb_build_object('totalPrice', total_price::numeric)) 

The result is {"totalPrice":{"totalPrice":1000}} which is unexpected

How can I set the totalPrice property correctly?

1 Answer 1

1

You don't need the JSONB_SET, just use JSONB_BUILD_OBJECT:

UPDATE orders 
SET price_detail = JSONB_BUILD_OBJECT('totalPrice', total_price::numeric)

Demo on dbfiddle

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

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.