Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y8wYR-00062t-NY for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2015 19:46:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y8wYR-0007wO-81 for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2015 19:46:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y8wYQ-0007wI-DS for pgsql-hackers@postgresql.org; Wed, 07 Jan 2015 19:46:06 +0000 Received: from relay5-d.mail.gandi.net ([2001:4b98:c:538::197]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Y8wYM-0001yf-Jf for pgsql-hackers@postgresql.org; Wed, 07 Jan 2015 19:46:05 +0000 Received: from mfilter30-d.gandi.net (mfilter30-d.gandi.net [217.70.178.161]) by relay5-d.mail.gandi.net (Postfix) with ESMTP id 82EB041C056; Wed, 7 Jan 2015 20:45:54 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter30-d.gandi.net Received: from relay5-d.mail.gandi.net ([217.70.183.197]) by mfilter30-d.gandi.net (mfilter30-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id O1aI4srF60pK; Wed, 7 Jan 2015 20:45:53 +0100 (CET) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 609D241C054; Wed, 7 Jan 2015 20:45:52 +0100 (CET) Message-ID: <54AD8CEF.3080904@dunslane.net> Date: Wed, 07 Jan 2015 14:45:51 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: Aaron Botsis , pgsql-hackers@postgresql.org CC: Francisco Olarte Subject: Re: Patch: [BUGS] BUG #12320: json parsing with embedded double quotes References: <87B56448-146A-4ECD-96D3-8B09B89D4EEA@bt-r.com> In-Reply-To: <87B56448-146A-4ECD-96D3-8B09B89D4EEA@bt-r.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 01/07/2015 08:25 AM, Aaron Botsis wrote: > Hi folks, I was having a problem importing json data with COPY. Lots=20 > of things export data nicely as one json blob per line. This is=20 > excellent for directly importing into a JSON/JSONB column for analysis. > > ...Except when there=E2=80=99s an embedded doublequote. Or anything that= =E2=80=99s=20 > escaped. COPY handles this, but by the time the escaped char hit the=20 > JSON parser, it's not escaped anymore. This breaks the JSON parsing.=20 > This means I need to manipulate the input data to double-escape it.=20 > See bug #12320 for an example. Yuck. > > I propose this small patch that simply allows specifying COPY =E2=80=A6 E= SCAPE=20 > without requiring the CSV parser. It will make it much easier to=20 > directly use json formatted export data for folks going forward. This=20 > seemed like the simplest route. > > Usage is simply: > > postgres=3D# copy t1 from '/Users/nok/Desktop/queries.json'; > ERROR: invalid input syntax for type json > DETAIL: Token "root" is invalid. > CONTEXT: JSON data, line 1: ...1418066241619 AND <=3D1418671041621) AND= =20 > user:"root... > COPY t1, line 3, column bleh:=20 > "{"timestamp":"2014-12-15T19:17:32.505Z","duration":7.947,"query":{"query= ":{"filtered":{"filter":{"qu..." > postgres=3D# copy t1 from '/Users/nok/Desktop/queries.json' escape ''; > COPY 1966 > > This isn't a bug. Neither CSV format nor TEXT format are partucularly=20 suitable for json. I'm quite certain I could compose legal json that=20 will break your proposal (for example, with an embedded newline in the=20 white space.) It's also unnecessary. CSV format, while not designed for this, is=20 nevertheless sufficiently flexible to allow successful import of json=20 data meeting certain criteria (essentially no newlines), like this: copy the_table(jsonfield) from '/path/to/jsondata' csv quote e'\x01' delimiter e'\x02'; You aren't the first person to encounter this problem. See=20 Maybe we need to add something like this to the docs, or to the wiki. Note too my comment in that blog post: Now this solution is a bit of a hack. I wonder if there's a case for a COPY mode that simply treats each line as a single datum. I also wonder if we need some more specialized tools for importing JSON, possibly one or more Foreign Data Wrappers. Such things could handle, say, embedded newline punctuation. cheers andrew --=20 Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers