Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y9cnz-0003ZA-1B for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jan 2015 16:52:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y9cny-00012J-EF for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jan 2015 16:52:58 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y9JKc-0002pS-AE for pgsql-hackers@postgresql.org; Thu, 08 Jan 2015 20:05:22 +0000 Received: from mail.digitalmafia.org ([162.243.67.207]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y9JKS-0006RB-Tn for pgsql-hackers@postgresql.org; Thu, 08 Jan 2015 20:05:19 +0000 Received: from [172.16.102.197] (ip-207-145-81-212.nyc.megapath.net [207.145.81.212]) by mail.digitalmafia.org (Postfix) with ESMTPSA id E0E4FA6599; Thu, 8 Jan 2015 15:05:10 -0500 (EST) Content-Type: multipart/alternative; boundary="Apple-Mail=_C54FBCF4-B566-4170-95DF-41510E9F9BED" Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) Subject: Re: Patch: [BUGS] BUG #12320: json parsing with embedded double quotes From: Aaron Botsis In-Reply-To: <54AD8CEF.3080904@dunslane.net> Date: Thu, 8 Jan 2015 15:05:10 -0500 Cc: Francisco Olarte Message-Id: References: <87B56448-146A-4ECD-96D3-8B09B89D4EEA@bt-r.com> <54AD8CEF.3080904@dunslane.net> To: Andrew Dunstan , pgsql-hackers@postgresql.org X-Mailer: Apple Mail (2.1993) X-Pg-Spam-Score: -1.9 (-) 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 --Apple-Mail=_C54FBCF4-B566-4170-95DF-41510E9F9BED Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jan 7, 2015, at 2:45 PM, Andrew Dunstan = wrote: >=20 >=20 > On 01/07/2015 08:25 AM, Aaron Botsis wrote: >> Hi folks, I was having a problem importing json data with COPY. Lots = of things export data nicely as one json blob per line. This is = excellent for directly importing into a JSON/JSONB column for analysis. >>=20 >> ...Except when there=E2=80=99s an embedded doublequote. Or anything = that=E2=80=99s escaped. COPY handles this, but by the time the escaped = char hit the JSON parser, it's not escaped anymore. This breaks the JSON = parsing. This means I need to manipulate the input data to double-escape = it. See bug #12320 for an example. Yuck. >>=20 >> I propose this small patch that simply allows specifying COPY =E2=80=A6= ESCAPE without requiring the CSV parser. It will make it much easier to = directly use json formatted export data for folks going forward. This = seemed like the simplest route. >=20 > This isn't a bug. Neither CSV format nor TEXT format are partucularly = suitable for json. I'm quite certain I could compose legal json that = will break your proposal (for example, with an embedded newline in the = white space.) Sorry - though I originally reported it as a bug, I didn=E2=80=99t mean = to imply it ultimately was one. :) The patch is a feature enhancement. > It's also unnecessary. CSV format, while not designed for this, is = nevertheless sufficiently flexible to allow successful import of json = data meeting certain criteria (essentially no newlines), like this: >=20 > copy the_table(jsonfield) > from '/path/to/jsondata' > csv quote e'\x01' delimiter e'\x02=E2=80=99; While perhaps unnecessary, given the size and simplicity of the patch, = IMO it=E2=80=99s a no brainer to merge (it actually makes the code = smaller by 3 lines). It also enables non-json use cases anytime one = might want to preserve embedded escapes, or use different ones entirely. = Do you see other reasons not to commit it? > 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. In your post you acknowledged a text mode copy with null escape = character would have solved your problem, and to me, that was the = intuitive/obvious choice as well. *shrug* > Note too my comment in that blog post: >=20 > 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. Agree. Though https://github.com/citusdata/json_fdw = already exists (I haven=E2=80=99t = used it). How do folks feel about a COPY mode that reads a single datum = until it finds a single character record terminator alone on a line? = something like: =3D# COPY test(data) from stdin terminator =E2=80=98}=E2=80=99; End with a backslash and a period on a line by itself. >>{ >> "a": 123, "c": "string", "b": [1, 2, 3 >> ] >>} >>{ >> [1,2,3] >>} >>\. COPY 2 You could also get fancy and support multi-character record terminators = which would allow the same thing in a slightly different way: COPY test(data) from stdin terminator =E2=80=98\n}=E2=80=99; COPY test(data) from stdin terminator =E2=80=98\n}=E2=80=99; I don=E2=80=99t know how crazy you could get without a lot of rework. It = might have to be used in conjunction with a more constrained mode like = "COPY=E2=80=A6RAW=E2=80=9D. Aaron= --Apple-Mail=_C54FBCF4-B566-4170-95DF-41510E9F9BED Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On Jan 7, 2015, at 2:45 PM, Andrew Dunstan <andrew@dunslane.net>= wrote:


On 01/07/2015 08:25 AM, = Aaron Botsis wrote:
Hi folks, I was having = a problem importing json data with COPY. Lots of things export data = nicely as one json blob per line. This is 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 escaped. COPY handles this, but by the time the = escaped char hit the JSON parser, it's not escaped anymore. This breaks = the JSON parsing. This means I need to manipulate the input data to = double-escape it. See bug #12320 for an example. Yuck.

I propose this small patch that simply allows specifying COPY = =E2=80=A6 ESCAPE without requiring the CSV parser. It will make it much = easier to directly use json formatted export data for folks going = forward. This seemed like the simplest route.

This isn't a bug. Neither CSV = format nor TEXT format are partucularly suitable for json. I'm quite = certain I could compose legal json that will break your proposal (for = example, with an embedded newline in the white space.)

Sorry - = though I originally reported it as a bug, I didn=E2=80=99t mean to imply = it ultimately was one. :) The patch is a feature = enhancement.

It's also = unnecessary. CSV format, while not designed for this, is nevertheless = sufficiently flexible to allow successful import of json data meeting = certain criteria (essentially no newlines), like this:

  copy = the_table(jsonfield)
  from = '/path/to/jsondata'
  csv quote = e'\x01' delimiter e'\x02=E2=80=99;

While = perhaps unnecessary, given the size and simplicity of the patch, IMO = it=E2=80=99s a no brainer to merge (it actually makes the code smaller = by 3 lines). It also enables non-json use cases anytime one might want = to preserve embedded escapes, or use different ones entirely. Do you see = other reasons not to commit it?



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.

Agree. = Though https://github.com/citusdata/json_fdw already exists (I = haven=E2=80=99t used it). How do folks feel about a COPY mode that reads = a single datum until it finds a single character record terminator alone = on a line? something like:

=3D# COPY test(data) from stdin terminator = =E2=80=98}=E2=80=99;
End with a backslash and a period on a line by = itself.
>>{
>>  "a": 123, "c": "string",  "b": [1, 2, = 3
>> = ]
>>}
>>{
>> [1,2,3]
>>}
>>\.
COPY = 2

You could = also get fancy and support multi-character record terminators which = would allow the same thing in a slightly different way:
COPY = test(data) from stdin terminator =E2=80=98\n}=E2=80=99;

COPY test(data) = from stdin terminator =E2=80=98\n}=E2=80=99;

I don=E2=80=99t know how crazy you could get = without a lot of rework. It might have to be used in conjunction with a = more constrained mode like "COPY=E2=80=A6RAW=E2=80=9D.

Aaron
= --Apple-Mail=_C54FBCF4-B566-4170-95DF-41510E9F9BED--