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 1f5Ebd-0000k7-2S for pgsql-general@arkaria.postgresql.org; Sun, 08 Apr 2018 17:59:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f5Eba-0006gb-0d for pgsql-general@arkaria.postgresql.org; Sun, 08 Apr 2018 17:59:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5EbX-0006gS-LA for pgsql-general@lists.postgresql.org; Sun, 08 Apr 2018 17:59:53 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5EbT-0007l8-Js for pgsql-general@lists.postgresql.org; Sun, 08 Apr 2018 17:59:50 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id D0A7A2096D; Sun, 8 Apr 2018 13:59:45 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 08 Apr 2018 13:59:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=/MpOuGf2c2hUVSnR7+XJRKpIXc+6L LJ8cMiqMQ2CWno=; b=BXJNaYXVy1mgQoFlmXX3ue5y55BYsDdY28Ik7kg+CgSdT Eerw2YguZNltF62+Ub6M9JaAFTeL8HedUZ+Z8dd7EJFBd9Cybc4uhjsEbaRz51xk RFW0bQTLVkYCZZyly1AZjkFljyJ7BehQ4sXmt3NVec9E2xdf2dIRJrJT5+WtPmkV EVci8ETr4nCsUBqQb0FgyDOI2nkWio9YbBskrx8Z8rW2DeccvdRmbJ+ykNXb0S0g XHXGf0+UrWxva1Jnr5ZrgmIZC0xzbdwTF76+FA40cR5rEYjCh5qcUtVPyNEwvyEp Nu/2Ge4BJSu1NNA0AfZoGy9L5F5OcexHW8D6JOOoQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=/MpOuG f2c2hUVSnR7+XJRKpIXc+6LLJ8cMiqMQ2CWno=; b=iMxgkOy/q9oiPD06UuL6AV 7JFYcshVMz7ec404tXn6ApwxRjrQNlQavH64b9WDCePu6eo3XDOyq/+6hOMrerrz jkQ6QIYFablKrtRbLdYru7pP9clc0TCRHRlZuxfQ2qmh7i6ToJlLCXSaDrzSIkhv HX2fO9uHWuRvH2+wbEls3ym5eKbnbMn0urUSlE0atDSHXXcV2Sz0lFsw6ztOtZJo 78zdetJSXkqFsKuG29xQCChrzkllVmptvXyF6tGg9t54GZpX9VgjPeAtvzSdnv5K xBrzDkqQEe3im8oL1O+XFWVDxw1FDF9Azoy02Ha+EpRjcXxU1qW037DNvhUT/EPw == X-ME-Sender: Received: from [192.168.1.2] (71-35-123-124.tukw.qwest.net [71.35.123.124]) by mail.messagingengine.com (Postfix) with ESMTPA id 486F81025B; Sun, 8 Apr 2018 13:59:45 -0400 (EDT) Subject: Re: Conflict between JSON_AGG and COPY To: =?UTF-8?B?xJDhu5cgTmfhu41jIFRyw60gQ8aw4budbmc=?= , pgsql-general References: <152647427.13167958.1523097853639.JavaMail.zimbra@digi-texx.vn> From: Adrian Klaver Message-ID: Date: Sun, 8 Apr 2018 10:59:44 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <152647427.13167958.1523097853639.JavaMail.zimbra@digi-texx.vn> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/07/2018 03:44 AM, Đỗ Ngọc Trí Cường wrote: > Dear 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 | username |   fullname > ----+-------------+--------------- >   1 | john        | John >   2 | anna        | Anna >   3 | sussi        | Sussi >   4 | david      | David Beckham >   5 | wayne     | Wayne 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","fullname":"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 the same data in the other way: > > COPY (WITH t2 AS (select row_to_json(t) as js from test_table t), >         t1 AS (SELECT ARRAY_TO_JSON(array_agg(js)) as "RECORDS" > FROM t2) >     SELECT row_to_json(t1) FROM t1) >     TO '/home/postgres/test2.json'; CREATE TABLE test_table (id integer, username varchar, fullname varchar); INSERT INTO test_table VALUES (1, 'john', 'John'), (2, 'anna', 'Anna'), (3, 'sussi', 'Sussi'), (4, 'david', 'David Beckham'), (5, 'wayne', 'Wayne Rooney'); This can be shortened to: COPY (select array_to_json(array_agg(row_to_json(t))) FROM test_table AS t) TO '/home/postgres/test2.json'; > > And the result 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,"username":"david","fullname":"David > Beckham"},{"id":5,"username":"wayne","fullname":"Wayne Rooney"}]} > > > I think the COPY command does not the `\n` character for pretty in > `json_agg` command. Well in the first case you are starting by concatenating the 5 rows in the table into a single row with the table rows separated by new lines: SELECT json_agg(t1) AS "RECORDS" FROM test_table t1; 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":"wayne","fullname":"Wayne Rooney"}] (1 row) In the second case you start by maintaining the separate table rows: select row_to_json(t) as js from test_table t; js -------------------------------------------------------- {"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":"wayne","fullname":"Wayne Rooney"} (5 rows) and then keeping that as an array of arrays: select array_agg(row_to_json(t)) from test_table t; {"{\"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\":\"wayne\",\"fullname\":\"Wayne Rooney\"}"} (1 row) which then gets turned back into JSON: select ARRAY_TO_JSON(array_agg(row_to_json(t))) from test_table t; [{"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":"wayne","fullname":"Wayne Rooney"}] > > Please help me give me your idea. Am I wrong or this is really a bug? > > Thank you and best regards, > > Đỗ Ngọc Trí*Cường*(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 > privileged information. If you are not the intended recipient, please > delete it and notify the sender immediately. Any unauthorized copying, > disclosure or distribution of the material in this e-mail is strictly > forbidden./ > > /*Please consider the environment before printing./ > > -- Adrian Klaver adrian.klaver@aklaver.com