{"id":1982,"date":"2017-10-14T19:01:38","date_gmt":"2017-10-14T16:01:38","guid":{"rendered":"http:\/\/www.profelis.com.tr\/tr\/blog\/?p=1982"},"modified":"2023-02-01T10:58:00","modified_gmt":"2023-02-01T07:58:00","slug":"postgresql-json-veri-tipi-islemleri","status":"publish","type":"post","link":"https:\/\/profelis.com.tr\/en\/2017\/10\/14\/postgresql-json-veri-tipi-islemleri\/","title":{"rendered":"PostgreSQL &#8211; JSON Veri Tipi \u0130\u015flemleri"},"content":{"rendered":"<h2>PostgreSQL &#8211; JSON Veri Tipi \u0130\u015flemleri<\/h2>\n<p>JSON veri tipi ve destekleyici i\u015flevler PostgreSQL 9.2 s\u00fcr\u00fcm\u00fc ile desteklenmeye ba\u015flad\u0131\u011f\u0131nda b\u00fcy\u00fck olay olmu\u015ftu. Bug\u00fcn ise PostgreSQL&#8217;in ard\u0131ndan di\u011fer veritabanlar\u0131 da birer birer bu veri tipine destek vermeye ba\u015flarken, PostgreSQL&#8217;de oturmu\u015f bir json deste\u011fi mevcut. <a href=\"http:\/\/json.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">JSON<\/a>, web uygulamalar\u0131, JavaScript ve REST tabanl\u0131 mobil uygulama geli\u015ftirenler i\u00e7in vazge\u00e7ilmez bir dil hatta eskilerin deyimiyle sabir dil (<em><strong>lingua franca: <\/strong>ge\u00e7erli dil, ortak dil<\/em>) durumunda. 9.4 s\u00fcr\u00fcm\u00fcnde ise JSON&#8217;\u0131n binary s\u00fcr\u00fcm\u00fc olan jsonb veri tipi deste\u011fi gelmi\u015fti.<\/p>\n<h2>JSON Fonksiyon ve Operat\u00f6rleri<\/h2>\n<p>T\u00fcm JSON fonksiyon ve operat\u00f6rleri i\u00e7in\u00a0<a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/functions-json.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.postgresql.org\/docs\/current\/static\/functions-json.html<\/a>\u00a0adresine bakabilirsiniz.<\/p>\n<h2>JSON Veri Eklemek<\/h2>\n<p>\u00d6nce veri tipi json olan bir s\u00fctun i\u00e7eren bir tablo olu\u015ftural\u0131m:<\/p>\n<pre><code>CREATE TABLE aile (id serial PRIMARY KEY, profil json);<\/code><\/pre>\n<p>Sonra bu tabloya json veri ekleyelim:<\/p>\n<pre><code>\nINSERT INTO aile (profil) VALUES ('\n{{\"ad\": \"Katip\", \n  \"fertler\": [ \n    {\"fert\": {\"ili\u015fki\": \"baba\", \"ad\": \"H\u00fcseyin\" }}, \n    {\"fert\": {\"ili\u015fki\": \"anne\", \"ad\": \"Saniye\" }}, \n    {\"fert\": {\"ili\u015fki\": \"\u00e7ocuk\", \"ad\": \"Emrah\" }}, \n    {\"fert\": {\"ili\u015fki\": \"\u00e7ocuk\", \"ad\": \"Sema\" }}]}\n');\n<\/code><\/pre>\n<p>PostgreSQL tabloya veriyi eklemeden \u00f6nce JSON veriyi do\u011frulamaktad\u0131r.<\/p>\n<div class=\"alert alert-danger\"><strong>Dikkat!<\/strong> Ge\u00e7erli olmayan bir JSON veriyi json veri tipi olarak i\u015faretlenmi\u015f bir s\u00fctuna yerle\u015ftiremezsiniz!<\/div>\n<h2>JSON Veri Sorgulama<\/h2>\n<p>A\u015fa\u011f\u0131daki sorgu\u00a0 json_extract_path, json_array_elements, ve json_extract_path_text i\u015flevlerini kullanarak aile \u00fcyelerini getirmektedir.<\/p>\n<p>Sorguyu par\u00e7alay\u0131p anlatmaya \u00e7al\u0131\u015faca\u011f\u0131m:<\/p>\n<pre><code>SELECT \njson_extract_path_text(profil, 'ad') AS aile,\njson_extract_path_text(json_array_elements (json_extract_path(profil,'fertler')), \n'fert','ad') As fert \nFROM aile;\n--\n aile     | fert\n----------+--------- \nKatip     | H\u00fcseyin\nKatip     | Saniye\nKatip     | Emrah \nKatip     | Sema\n<\/code><\/pre>\n<p>SELECT<br \/>\njson_extract_path_text(profil, &#8216;ad&#8217;) AS aile,<span class=\"label label-quaternary\">1<\/span><br \/>\njson_extract_path_text( <span class=\"label label-quaternary\">2<\/span> json_array_elements ( <span class=\"label label-quaternary\">3<\/span> json_extract_path(profil,&#8217;fertler&#8217;) <span class=\"label label-quaternary\">4<\/span> ), &#8216;fert&#8217;,&#8217;ad&#8217;) As fert FROM aile;<\/p>\n<ol class=\"list list-ordened list-ordened-style-3\">\n<li>Aile ad\u0131n\u0131 text olarak getirir<\/li>\n<li>Aile ferdi ad\u0131n\u0131 text olarak getirir<\/li>\n<li>Array elemanlar\u0131n\u0131 ayr\u0131 JSON objeleri olarak getirir<\/li>\n<li>Aile fertlerini ayr\u0131 objeler olarak getirir<\/li>\n<\/ol>\n<p>Bu \u015fekilde yazmak yerine fonksiyonlar\u0131n k\u0131sayol operat\u00f6rlerini kullanmak daha kolay geliyor bana, muhtemelen obje tabanl\u0131 programlama yapanlara da bu daha do\u011fal gelecektir. Yukar\u0131daki sorgu bu durumda a\u015fa\u011f\u0131daki gibi daha kolay hale geliyor:<\/p>\n<pre><code>SELECT profile-&gt;&gt;'name' As family, \n  json_array_elements((profile-&gt;'members')) #&gt;&gt; '{member,name}'::text[] AS member \nFROM families_j;\n<\/code><\/pre>\n<h2>JSON Sonu\u00e7 D\u00f6nd\u00fcrmek<\/h2>\n<p><strong>row_to_json<\/strong> fonksiyonu ile se\u00e7ilen s\u00fctunlar\u0131 JSON format\u0131nda \u00e7\u0131kt\u0131 haline getirmek m\u00fcmk\u00fcnd\u00fcr.<\/p>\n<pre><code>select row_to_json(words) from words;<\/code><\/pre>\n<p>Not: olarak PostgreSQL ayn\u0131 zamanda di\u011fer veritabanlar\u0131 gibi XML de desteklemektir.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON veri tipi ve destekleyici i\u015flevler PostgreSQL 9.2 s\u00fcr\u00fcm\u00fc ile desteklenmeye ba\u015flad\u0131\u011f\u0131nda b\u00fcy\u00fck olay olmu\u015ftu. Bug\u00fcn ise PostgreSQL&#8217;in ard\u0131ndan di\u011fer veritabanlar\u0131 da birer birer bu veri tipine destek vermeye ba\u015flarken, PostgreSQL&#8217;de oturmu\u015f bir json deste\u011fi mevcut. JSON, web uygulamalar\u0131, JavaScript ve REST tabanl\u0131 mobil uygulama geli\u015ftirenler i\u00e7in vazge\u00e7ilmez bir dil hatta eskilerin deyimiyle sabir dil (lingua franca: ge\u00e7erli dil, ortak dil) durumunda. 9.4 s\u00fcr\u00fcm\u00fcnde ise JSON&#8217;\u0131n binary s\u00fcr\u00fcm\u00fc olan jsonb veri tipi deste\u011fi gelmi\u015fti.<\/p>","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,45],"tags":[48,60],"class_list":["post-1982","post","type-post","status-publish","format-standard","hentry","category-postgresql","category-yazi","tag-enterprisedb","tag-postgresql"],"_links":{"self":[{"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/posts\/1982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/comments?post=1982"}],"version-history":[{"count":0,"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/posts\/1982\/revisions"}],"wp:attachment":[{"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/media?parent=1982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/categories?post=1982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/profelis.com.tr\/en\/wp-json\/wp\/v2\/tags?post=1982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}