Search Postgresql Archives

JSON objects merge using || operator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I've encountered a disturbing behaviour using the || operator on two jsonb objects extracted from subfields of a common jsonb object.

Let's take a look at this example :

with data as (
    select '{
             "nested1": {"lvl1_k1": "v1"},
             "nested2": {"lvl2_k1":234, "lvl2_k2": "test"}
            }'::jsonb as extra_values,
    
    '{"aaa": 12}'::jsonb as j1,
    '{"bbb": "azerty", "ccc": "qwerty"}'::jsonb as j2
)
select    COALESCE(extra_values->'nested1', '{}')
       || COALESCE(extra_values->'nested2', '{}') as correct,
       
       extra_values->'nested1' || extra_values->'nested2' as bad,
       
       j1 || j2 as correct2
from data
;


I'm expecting to get the same result in columns 'correct' and 'bad', which is :
{"lvl1_k1": "v1", "lvl2_k1": 234, "lvl2_k2": "test"}

But what I'm getting in column 'bad' is only the right operand :
{"lvl2_k1": 234, "lvl2_k2": "test"}

I can recover to my expected behaviour by forcing a cast to jsonb on the second operand :

SELECT extra_values->'nested1' || (extra_values->'nested2')::jsonb

What's your opinion about this ? Is it a bug or an expected behaviour, and if so, how do you explain it ?

Thanks for sharing your knowledge !

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux