postgresql 9.5 版本中JSONB資料類型新增的一些函數與功能

來源:互聯網
上載者:User

標籤:

JSONB-modifying operators and functions

In 9.3 (and to a greater extent in 9.4), JSONB data could be extracted using various functions and operators, but nothing that could actually modify the data. As of 9.5, JSONB data can now be modified.
jsonb || jsonb (concatenate / overwrite)

The || operator allows us to combine 2 jsonb objects. If there‘s overlap, values are replaced on the highest level.

For example, if we want to add values to a jsonb object:

# SELECT ‘{"name": "Joe", "age": 30}‘::jsonb || ‘{"town": "London"}‘::jsonb;
                    ?column?                   
----------------------------------------------
  {"age": 30, "name": "Joe", "town": "London"}
(1 row)


Or we can overwrite existing values:

# SELECT ‘{"town": "Dataville", "population": 4096}‘::jsonb || ‘{"population": 8192}‘::jsonb;
                  ?column?                  
-------------------------------------------
  {"town": "Dataville", "population": 8192}
(1 row)

Note that this only works on the highest level, so nested objects are replaced from the top level. For example:

# SELECT ‘{"name": "Jane", "contact": {"phone": "01234 567890", "mobile": "07890 123456"}}‘::jsonb || ‘{"contact": {"fax": "01987 654321"}}‘::jsonb;
                        ?column?                       
------------------------------------------------------
  {"name": "Jane", "contact": {"fax": "01987 654321"}}
(1 row)


jsonb - text / int (remove key / array element)

We can remove keys from a jsonb object with the - operator:

  # SELECT ‘{"name": "James", "email": "[email protected]"}‘::jsonb - ‘email‘;
       ?column?      
  -------------------
   {"name": "James"}
  (1 row)

Or remove values from an array (base 0):

# SELECT ‘["red","green","blue"]‘::jsonb - 1;
     ?column?     
-----------------
  ["red", "blue"]
(1 row)

jsonb - text[] / int (remove key / array element in path)

The previous example, we can remove keys or array elements, but not any lower than the highest level, so we can provide a path to the value we want to delete using a text array. Here we‘ll want to remove the fax number from within the contact value:

# SELECT ‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb - ‘{contact,fax}‘::text[];
                         ?column?                         
---------------------------------------------------------
  {"name": "James", "contact": {"phone": "01234 567890"}}
(1 row)

Or we can remove an array value. Here we‘ll get rid of the array value as index 1 (2nd value):

# SELECT ‘{"name": "James", "aliases": ["Jamie","The Jamester","J Man"]}‘::jsonb - ‘{aliases,1}‘::text[];
                      ?column?                     
--------------------------------------------------
  {"name": "James", "aliases": ["Jamie", "J Man"]}
(1 row)

jsonb_replace function

The above lets us delete values in a path, but not update them, so we have the jsonb_replace function for that. We‘ll update the phone value within the contact value:

# SELECT jsonb_replace(‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb, ‘{contact,phone}‘, ‘"07900 112233"‘::jsonb);
                                  jsonb_replace                                  
--------------------------------------------------------------------------------
  {"name": "James", "contact": {"fax": "01987 543210", "phone": "07900 112233"}}
(1 row)

jsonb_pretty

Notice that jsonb doesn‘t preserve white-space, so no matter how effort you went to in order to make the object easier to read, it will end up as a long string. Well jsonb_pretty will format it for you. If we use the previous jsonb example and wrap it all in a jsonb_pretty function:

# SELECT jsonb_pretty(jsonb_replace(‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb, ‘{contact,phone}‘, ‘"07900 112233"‘::jsonb));
           jsonb_pretty           
---------------------------------
  {                              +
      "name": "James",           +
      "contact": {               +
          "fax": "01987 543210", +
          "phone": "07900 112233"+
      }                          +
  }
(1 row)

Much easier to read.

postgresql 9.5 版本中JSONB資料類型新增的一些函數與功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.