Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f4zhA-0003He-2a for pgsql-general@arkaria.postgresql.org; Sun, 08 Apr 2018 02:04:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f4zh8-00052e-A1 for pgsql-general@arkaria.postgresql.org; Sun, 08 Apr 2018 02:04:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f4lK9-0004Uf-Uv for pgsql-general@lists.postgresql.org; Sat, 07 Apr 2018 10:43:57 +0000 Received: from mail.digi-texx.vn ([116.193.72.250]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f4lK1-0002fu-Ah for pgsql-general@lists.postgresql.org; Sat, 07 Apr 2018 10:43:56 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.digi-texx.vn (Postfix) with ESMTP id 170F182C580E for ; Sat, 7 Apr 2018 17:46:38 +0700 (+07) Received: from mail.digi-texx.vn ([127.0.0.1]) by localhost (mx01.digi-texx.vn [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id M-cy_I2J0CS1 for ; Sat, 7 Apr 2018 17:46:37 +0700 (+07) Received: from localhost (localhost [127.0.0.1]) by mail.digi-texx.vn (Postfix) with ESMTP id 7F89A82C580D for ; Sat, 7 Apr 2018 17:46:37 +0700 (+07) X-Virus-Scanned: amavisd-new at mx01.digi-texx.vn Received: from mail.digi-texx.vn ([127.0.0.1]) by localhost (mx01.digi-texx.vn [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id vKxRzSqjoGmJ for ; Sat, 7 Apr 2018 17:46:37 +0700 (+07) Received: from zimbra-mail1.digi-texx.vn (zimbra-mail1.digi-texx.vn [10.1.1.150]) by mail.digi-texx.vn (Postfix) with ESMTP id 5FE1882C580E for ; Sat, 7 Apr 2018 17:46:37 +0700 (+07) Date: Sat, 7 Apr 2018 17:44:13 +0700 (ICT) From: =?utf-8?B?xJDhu5cgTmfhu41jIFRyw60gQ8aw4budbmc=?= Reply-To: =?utf-8?B?xJDhu5cgTmfhu41jIFRyw60gQ8aw4budbmc=?= To: pgsql-general Message-ID: <152647427.13167958.1523097853639.JavaMail.zimbra@digi-texx.vn> Subject: Conflict between JSON_AGG and COPY MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_13167957_1446175009.1523097853638" X-Mailer: Zimbra 8.6.0_GA_1153 (ZimbraWebClient - FF47 (Win)/8.6.0_GA_1153) Thread-Topic: Conflict between JSON_AGG and COPY Thread-Index: jWTpUvVKTBrD2qyYIe9kycJsP5JQbQ== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_13167957_1446175009.1523097853638 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Dear all,=20 I've found one case. I don't know this is a bug or I config/query some thin= gs wrong.=20 Let I describe it. I have a table with structure and data is:=20 id | username | fullname=20 ----+-------------+---------------=20 1 | john | John=20 2 | anna | Anna=20 3 | sussi | Sussi=20 4 | david | David Beckham=20 5 | wayne | Wayne Rooney=20 I want to export it to a file in JSON format so I run the query as below:= =20 COPY (SELECT row_to_json(t) FROM (SELECT json_agg(t1) AS "RECORDS" FROM tes= t_table t1) t) TO '/home/postgres/test1.json';=20 But the result I got will include "\n" in the result:=20 BQ_BEGIN {"RECORDS":[{"id":1,"username":"john","fullname":"John"}, \n {"id":2,"usern= ame":"anna","fullname":"Anna"}, \n {"id":3,"username":"sussi","fullname":"S= ussi"}, \n {"id":4,"username":"david","fullname":"David Beckham"}, \n {"id"= :5,"username":"wayne","fullname":"Wayne Rooney"}]}=20 BQ_END Then, I try to get the same data in the other way:=20 BQ_BEGIN COPY (WITH t2 AS (select row_to_json(t) as js from test_table t),=20 t1 AS (SELECT ARRAY_TO_JSON(array_agg(js)) as "RECORDS" FROM t2)=20 SELECT row_to_json(t1) FROM t1)=20 TO '/home/postgres/test2.json';=20 BQ_END And the result I got is quite match what I expect.=20 BQ_BEGIN {"RECORDS":[{"id":1,"username":"john","fullname":"John"},{"id":2,"username"= :"anna","fullname":"Anna"},{"id":3,"username":"sussi","fullname":"Sussi"},{= "id":4,"username":"david","fullname":"David Beckham"},{"id":5,"username":"w= ayne","fullname":"Wayne Rooney"}]}=20 BQ_END I think the COPY command does not the `\n` character for pretty in `json_ag= g` command.=20 Please help me give me your idea. Am I wrong or this is really a bug?=20 Thank you and best regards,=20 =C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB=9Dng (Mr.)=20 Software Development Dept. =09 Mobile: +84 9 3800 3394=20 Phone: +84 28 3715 6322=20 Email: dntcuong@digi-texx.vn=20 DIGI-TEXX | a global BPO provider=20 Address : Anna Building, Quang Trung Software City,=20 District. 12, Ho Chi Minh City, Vietnam=20 Website: www.digi-texx.vn=20 IMPORTANT NOTICE:=20 *This e-mail and any attachments may contain confidential and/or privileged= information. If you are not the intended recipient, please delete it and n= otify the sender immediately. Any unauthorized copying, disclosure or distr= ibution of the material in this e-mail is strictly forbidden.=20 *Please consider the environment before printing.=20 ------=_Part_13167957_1446175009.1523097853638 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
D= ear all,

I've found one case. I don't know this is a bug = or I config/query some things wrong.

Let I describe it. I= have a table with structure and data is:

 id | user= name |   fullname
----+-------------+---------------
 = 1 | john        | John
  2 | anna &n= bsp;      | Anna
  3 | sussi  &n= bsp;     | Sussi
  4 | david   &= nbsp;  | David Beckham
  5 | wayne     | W= ayne Rooney

I want to export it to a file in JSON format = so I run the query as below:
COPY (SELECT row_to_= json(t) FROM (SELECT json_agg(t1) AS "RECORDS" FROM test_table t1) t) TO '/= home/postgres/test1.json';
But the result I got will= include "\n" in the result:
{"RECORDS":[{"id":1,= "username":"john","fullname":"John"}, \n {"id":2,"username":"anna","fullnam= e":"Anna"}, \n {"id":3,"username":"sussi","fullname":"Sussi"}, \n {"id":4,"= username":"david","fullname":"David Beckham"}, \n {"id":5,"username":"wayne= ","fullname":"Wayne Rooney"}]}
Then, I try to get th= e same data in the other way:
COPY (WITH t2 AS (s= elect row_to_json(t) as js from test_table t),
     =    t1 AS (SELECT ARRAY_TO_JSON(array_agg(js)) as "RECORDS" FROM t= 2)
    SELECT row_to_json(t1) FROM t1)
   &= nbsp;TO '/home/postgres/test2.json';
And the res= ult I got is quite match what I expect.
{"RECORDS= ":[{"id":1,"username":"john","fullname":"John"},{"id":2,"username":"anna","= fullname":"Anna"},{"id":3,"username":"sussi","fullname":"Sussi"},{"id":4,"u= sername":"david","fullname":"David Beckham"},{"id":5,"username":"wayne","fu= llname":"Wayne Rooney"}]}

I think the COPY c= ommand does not the `\n` character for pretty in `json_agg` command.

Please help me= give me your idea. Am I wrong or this is really a bug?

Thank you and best regards,

<= /tr>

=C4=90=E1=BB=97 Ng= =E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB=9Dng (Mr.)

Software Development Dept.

Mobile: +84 9 3800 3394

Phone: +84 28 3715 6322

Email: dntcuong@digi-texx.vn=

DIGI-TEXX | a global BPO provider

Address: Anna Building, Quang Trung Software City,

District. 12, Ho Chi Minh= City, Vietnam

Website: = www.digi-texx.vn

 

IMPORTANT NOTICE:

*This e-mail and any attachments may contain confidential and/or privile= ged information. If you are not the intended recipient, please delete it an= d notify the sender immediately. Any unauthorized copying, disclosure or di= stribution of the material in this e-mail is strictly forbidden.

*Pl= ease consider the environment before printing.


------=_Part_13167957_1446175009.1523097853638--