{"id":331440,"date":"2022-04-03T15:00:28","date_gmt":"2022-04-03T15:00:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=331440"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=331440","title":{"rendered":"<span>\u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 JSON \u0432 PostgreSQL<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c71\/ec1\/924\/c71ec1924b23fa69a871407661744234.jpg\" width=\"3960\" height=\"2472\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c71\/ec1\/924\/c71ec1924b23fa69a871407661744234.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 1. \u041a\u043e\u0440\u0442\u0435\u0436 \u043a\u043b\u044e\u0447: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435   <\/p>\n<pre><code class=\"sql\">select row_to_json(j)  from ( select  'value_1' as key_1, 'value_2' as key_2, 'value_3' as key_3 ) as j;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">{\"key_1\":\"value_1\",\"key_2\":\"value_2\",\"key_3\":\"value_3\"}<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 2. \u041c\u0430\u0441\u0441\u0438\u0432 JSON<\/p>\n<pre><code class=\"sql\">select array_to_json(array_agg(row_to_json(j))) from ( select  'value_1' as key_1, 'value_2' as key_2, 'value_3' as key_3 ) as j;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\":\"value_1\",\"key_2\":\"value_2\",\"key_3\":\"value_3\"}]<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 3. \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 JSON<\/p>\n<pre><code class=\"sql\">select array_to_json(array_agg(json_build_object(j,i)))  from  ( values  ('key_1', 'value_1'),  ('key_2', 'value_2'),  ('key_3', 'value_3') ) as t(j,i);<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\":\"value_1\"},{\"key_2\":\"value_2\"},{\"key_3\" \"value_3\"}]<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 4. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 JSON \u0438\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432  <\/p>\n<pre><code class=\"sql\">select json_build_object ( 'key_1','value_1', 'key_2', 'value_2', 'key_3', 'value_3' );<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">{\"key_1\" : \"value_1\", \"key_2\" : \"value_2\", \"key_3\" : \"value_3\"}<\/code><\/pre>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432<\/p>\n<pre><code class=\"sql\">create table some_table ( column_1 text, column_2 text, column_3 text, column_4 text, column_5 text, column_6 text );  insert into some_table ( column_1,  column_2,  column_3, column_4, column_5, column_6 ) values  ( 'value_1',  'value_2',  'value_3', 'value_4',  'value_5',  'value_6' );<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 5. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 JSON \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a\u043b\u044e\u0447\u0435\u0439 \u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439<\/p>\n<pre><code class=\"sql\">select json_object ( '{key_1, key_2, key_3}',  '{value_1, value_2, value_3}' );<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">{\"key_1\" : \"value_1\", \"key_2\" : \"value_2\", \"key_3\" : \"value_3\"}<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 6. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043a\u043b\u044e\u0447: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <\/p>\n<pre><code class=\"sql\">select json_agg(json_build_object (   'key_1', t.column_1,   'key_2', t.column_2,   'key_3', t.column_3,   'key_4', t.column_4,   'key_5', t.column_5,   'key_6', t.column_6 )) from some_table as t;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\" : \"value_1\", \"key_2\" : \"value_2\", \"key_3\" : \"value_3\", \"key_4\" : \"value_4\", \"key_5\" : \"value_5\", \"key_6\" : \"value_6\"}]<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 7. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043a\u043b\u044e\u0447: \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439<\/p>\n<pre><code class=\"sql\">select json_agg(json_build_object (     'key_1', json_build_array(t.column_1, column_2), 'key_2', json_build_array(t.column_3, column_4), 'key_2', json_build_array(t.column_5, column_6) )) from some_table as t;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\" : [\"value_1\", \"value_2\"], \"key_2\" : [\"value_3\", \"value_4\"], \"key_2\" : [\"value_5\", \"value_6\"]}]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/658817\/\"> https:\/\/habr.com\/ru\/post\/658817\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 1. \u041a\u043e\u0440\u0442\u0435\u0436 \u043a\u043b\u044e\u0447: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435   <\/p>\n<pre><code class=\"sql\">select row_to_json(j)  from ( select  'value_1' as key_1, 'value_2' as key_2, 'value_3' as key_3 ) as j;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">{\"key_1\":\"value_1\",\"key_2\":\"value_2\",\"key_3\":\"value_3\"}<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 2. \u041c\u0430\u0441\u0441\u0438\u0432 JSON<\/p>\n<pre><code class=\"sql\">select array_to_json(array_agg(row_to_json(j))) from ( select  'value_1' as key_1, 'value_2' as key_2, 'value_3' as key_3 ) as j;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\":\"value_1\",\"key_2\":\"value_2\",\"key_3\":\"value_3\"}]<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 3. \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 JSON<\/p>\n<pre><code class=\"sql\">select array_to_json(array_agg(json_build_object(j,i)))  from  ( values  ('key_1', 'value_1'),  ('key_2', 'value_2'),  ('key_3', 'value_3') ) as t(j,i);<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\":\"value_1\"},{\"key_2\":\"value_2\"},{\"key_3\" \"value_3\"}]<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 4. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 JSON \u0438\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432  <\/p>\n<pre><code class=\"sql\">select json_build_object ( 'key_1','value_1', 'key_2', 'value_2', 'key_3', 'value_3' );<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">{\"key_1\" : \"value_1\", \"key_2\" : \"value_2\", \"key_3\" : \"value_3\"}<\/code><\/pre>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432<\/p>\n<pre><code class=\"sql\">create table some_table ( column_1 text, column_2 text, column_3 text, column_4 text, column_5 text, column_6 text );  insert into some_table ( column_1,  column_2,  column_3, column_4, column_5, column_6 ) values  ( 'value_1',  'value_2',  'value_3', 'value_4',  'value_5',  'value_6' );<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 5. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 JSON \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a\u043b\u044e\u0447\u0435\u0439 \u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439<\/p>\n<pre><code class=\"sql\">select json_object ( '{key_1, key_2, key_3}',  '{value_1, value_2, value_3}' );<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">{\"key_1\" : \"value_1\", \"key_2\" : \"value_2\", \"key_3\" : \"value_3\"}<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 6. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043a\u043b\u044e\u0447: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <\/p>\n<pre><code class=\"sql\">select json_agg(json_build_object (   'key_1', t.column_1,   'key_2', t.column_2,   'key_3', t.column_3,   'key_4', t.column_4,   'key_5', t.column_5,   'key_6', t.column_6 )) from some_table as t;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\" : \"value_1\", \"key_2\" : \"value_2\", \"key_3\" : \"value_3\", \"key_4\" : \"value_4\", \"key_5\" : \"value_5\", \"key_6\" : \"value_6\"}]<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 7. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 \u043a\u043b\u044e\u0447: \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439<\/p>\n<pre><code class=\"sql\">select json_agg(json_build_object (     'key_1', json_build_array(t.column_1, column_2), 'key_2', json_build_array(t.column_3, column_4), 'key_2', json_build_array(t.column_5, column_6) )) from some_table as t;<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"json\">[{\"key_1\" : [\"value_1\", \"value_2\"], \"key_2\" : [\"value_3\", \"value_4\"], \"key_2\" : [\"value_5\", \"value_6\"]}]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/658817\/\"> https:\/\/habr.com\/ru\/post\/658817\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-331440","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331440"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331440\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}