Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKOUm-0002Ck-Ja for pgsql-general@arkaria.postgresql.org; Thu, 30 Nov 2017 13:03:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKOUl-000658-Bm for pgsql-general@arkaria.postgresql.org; Thu, 30 Nov 2017 13:03:15 +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.84_2) (envelope-from ) id 1eKOUk-00064y-TJ for pgsql-general@lists.postgresql.org; Thu, 30 Nov 2017 13:03:15 +0000 Received: from aibo.runbox.com ([91.220.196.211]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eKOUc-0000ou-Iy for pgsql-general@postgresql.org; Thu, 30 Nov 2017 13:03:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=rbselector1; h=Content-Type:MIME-Version:Message-ID:Date:Subject: In-Reply-To:References:To:From; bh=2rnQjMLaq7TizgQDaqm0VrAvDxAtIR463mrQZXmNStE=; b=MlKJnwplhCXBlIdVX0ElF2jAQ5 hGuGjuJSnph9cS7jnp0IfbRGi97dW8lJb4OaaCOL+bELmYESmebWITuG4H4GMRYOkrh3ucEMmWb4P zWiX9e95Dv5aOnAyJ4neJzDFCdzAc/akE5XiKX8ixIJ+6X/udWJTwyqzizgSwDOT9/wMjAo2iVOdr gtilzim72+V9e5G0boULT/jcf1n+WPDXfjUFoaphWr1Xxzbv8kwZJJ9+5RDQU1J4RLf67UAMZ+wm3 hCWIziYPFf6Mpci2Etip5EkoqSz34hnkAbJC2FWXUBcm8c/RVsmX9WGAx03Z44YoX6zsrmtdpPhaS qeNU6Wkw==; Received: from [10.9.9.210] (helo=mailfront10.runbox.com) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1eKOUY-0005OB-TO; Thu, 30 Nov 2017 14:03:03 +0100 Received: from cpe-66-27-125-35.san.res.rr.com ([66.27.125.35] helo=seasyslap4) by mailfront10.runbox.com with esmtpsa (uid:561468 ) (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.82) id 1eKOUM-0007Q9-U5; Thu, 30 Nov 2017 14:02:51 +0100 From: "Mike Sofen" To: "'tao tony'" , References: In-Reply-To: Subject: RE: copy error with json/jsonb types Date: Thu, 30 Nov 2017 05:02:46 -0800 Message-ID: <05eb01d369db$86700940$93501bc0$@runbox.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_05EC_01D36998.784E4FE0" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQH07U4H/7wiHLhHNQm+aNdt5gpQUqLp2UEg Content-Language: en-us List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_05EC_01D36998.784E4FE0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: tao tony [mailto:tonytao0505@outlook.com] Sent: Wednesday, November 29, 2017 10:02 PM I'm using copy and jdbc copyin to build load data to pg,data type in table is jsonb.postgresql version are 9.6 and 10.0. Some records with escape characters would be failed with error"ERROR: invalid input syntax for type json" but when using insert they could be executed successfully. please kindly tell me how to fix this issue? here is the table. hdb=# \d salejsonb Table "public.salejsonb" Column | Type | Collation | Nullable | Default ---------+-------+-----------+----------+--------- jsonval | jsonb | | | ====================================== I may be old school, but it would be rare situation where I'd be using an ETL process - and I consider the Copy function to be an extract and load part of that - to go directly into a live/final/production table. Instead, I "always" use a staging table that is much more tolerant of errors. In this case, I'd create a table with the one column with a datatype of "text". Now run the copy command, the data will land without error, then you can either devise processes that detect and fix invalid json or do a proper error out from the db, and on receiving proper json, proceeding to do a guaranteed good insert into your final jsonb table. If you were able to land the data in that staging table and then render it, you may be able to find a pattern in the invalid escape characters that you could fix on the fly on the insert from staging to final. Mike Sofen ------=_NextPart_000_05EC_01D36998.784E4FE0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

From:<= /b> = tao tony [mailto:tonytao0505@outlook.com]
Sent: Wednesday, = November 29, 2017 10:02 PM

=

I'm using copy = and jdbc copyin to build load data to pg,data type in table is = jsonb.postgresql version are 9.6 and 10.0.  Some records with escape characters would be = failed with error"ERROR:  invalid input syntax for type = json" but when using insert they = could be executed successfully.

please kindly = tell me how to fix this issue?

here is the = table.

hdb=3D# \d = salejsonb
          =    Table "public.salejsonb"
 Column  | = Type  | Collation | Nullable | Default =
---------+-------+-----------+----------+---------
 jsonval = | jsonb |           = |          | =

=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D

I may be old = school, but it would be rare situation where I’d be using an ETL = process – and I consider the Copy function to be an extract and = load part of that – to go directly into a live/final/production = table.

 

Instead, I = “always” use a staging table that is much more tolerant of = errors.  In this case, I’d create a table with the one column = with a datatype of “text”.   Now run the copy = command, the data will land without error, then you can either devise = processes that detect and fix invalid json or do a proper error out from = the db, and on receiving proper json, proceeding to do a guaranteed good = insert into your final jsonb table.  If you were able to land the = data in that staging table and then render it, you may be able to find a = pattern in the invalid escape characters that you could fix on the fly = on the insert from staging to final.

 

Mike = Sofen

------=_NextPart_000_05EC_01D36998.784E4FE0--