Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YmleW-0007DQ-HW for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Apr 2015 16:13:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YmleV-00067k-Sr for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Apr 2015 16:12:59 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YmleU-00067e-UB for pgsql-bugs@postgresql.org; Mon, 27 Apr 2015 16:12:59 +0000 Received: from mail-wi0-f181.google.com ([209.85.212.181]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YmleO-0005Dy-1u for pgsql-bugs@postgresql.org; Mon, 27 Apr 2015 16:12:57 +0000 Received: by wiun10 with SMTP id n10so95921981wiu.1 for ; Mon, 27 Apr 2015 09:12:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=dXDlNtG+vc27MZl+u1fhPCugOM/OKEarKz2KM6UVTcg=; b=ZFNsALX224bUNh0ejT6wGcnVb0uiW1DtEaz9DjHDRKV1BaEyC9AuFWXLm4f67yy+X2 gQLd3vHc4QFbMjiqqOOTso/UxRQJLieWXZDM4M3sudDGFmoRJ8hm2FeoADy8xtzq1rRv RTdDsEVL20lX0rjSaB+A3hjX4BxN+U++tI3guI3pQwqnAZKNlGTY4JDUcbkPukr1By3K 1GQYqrPaUvW1lv5i/d4CKCDdGMKqXwe4jtm/hvudSgDlHPbune56VABgOMA2e+/ToBCF NDkPROxxLrqKSm0t7JzdlBBGPfz+AYakAVrhDThiIecG9aUdWYZNOx7LufxaMHMifXxp 9G0Q== X-Gm-Message-State: ALoCoQk5aet84Ry5oLa9WpyNdGs9k0niXB7qgINGQW0wo2jMUfuXx19DwxX17SV5hDHXf9d/tYHE X-Received: by 10.180.96.138 with SMTP id ds10mr21125165wib.95.1430151170368; Mon, 27 Apr 2015 09:12:50 -0700 (PDT) Received: from Markos-MacBook-Pro.local ([46.246.117.202]) by mx.google.com with ESMTPSA id h5sm30031901wjn.20.2015.04.27.09.12.48 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Apr 2015 09:12:49 -0700 (PDT) Message-ID: <553E6000.4050702@joh.to> Date: Mon, 27 Apr 2015 18:12:48 +0200 From: Marko Tiikkaja User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: me@andrewray.me, pgsql-bugs@postgresql.org Subject: Re: BUG #13160: Incorrect escaping of quotes in JSON export References: <20150425195313.2561.68542@wrigleys.postgresql.org> In-Reply-To: <20150425195313.2561.68542@wrigleys.postgresql.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 4/25/15 9:53 PM, me@andrewray.me wrote: > Postgres escapes quotes incorrectly when creating a JSON export. > > UPDATE models SET column='"hello"' WHERE id=1; > > COPY (SELECT row_to_json(models) > FROM (SELECT column FROM shaders WHERE id=1) shaders) > TO '/output.json'; > > The contents of output.json: > > {"column":"\\"hello\\""} > > You can see that the quotes are escaped improperly and it creates invalid > JSON. It's not supposed to create valid JSON. It's supposed to generate something which can be COPYed back into a database. From the COPY documentation page: Backslash characters (\) can be used in the COPY data to quote data characters that might otherwise be taken as row or column delimiters. In particular, the following characters must be preceded by a backslash if they appear as part of a column value: backslash itself, newline, carriage return, and the current delimiter character. > It should be: > > {"column":"\"hello\""} That doesn't COPY back in: =# \copy qwr from /tmp/foo ERROR: invalid input syntax for type json DETAIL: Token "hello" is invalid. CONTEXT: JSON data, line 1: {"column":""hello... COPY qwr, line 1, column a: "{"column":""hello""}" COPY currently doesn't support what you're trying to do. I suggest using a simple program which connects to the database and writes the result column into a file verbatim. .m -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs