Combine two JSON objects in PostgreSQL

后端 未结 2 795
[愿得一人]
[愿得一人] 2020-12-29 11:53

I have two JSON rows in a PostgreSQL 9.4 table:

      the_column      
----------------------
 {\"evens\": [2, 4, 6]}
 {\"odds\": [1, 3, 5]}
<
2条回答
  •  心在旅途
    2020-12-29 12:25

    FYI, if someone's using jsonb in >= 9.5 and they only care about top-level elements being merged without duplicate keys, then it's as easy as using the || operator:

    select '{"evens": [2, 4, 6]}'::jsonb || '{"odds": [1, 3, 5]}'::jsonb;
                ?column?                 
    -----------------------------------------
    {"odds": [1, 3, 5], "evens": [2, 4, 6]}
    (1 row)
    

提交回复
热议问题