Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUPOm-0006Ij-2h for psycopg@arkaria.postgresql.org; Tue, 21 Feb 2023 09:57:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUPOk-00070T-DP for psycopg@arkaria.postgresql.org; Tue, 21 Feb 2023 09:57:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUOR9-0001f1-Go for psycopg@lists.postgresql.org; Tue, 21 Feb 2023 08:55:47 +0000 Received: from mout-p-101.mailbox.org ([80.241.56.151]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUOR6-00080P-N7 for psycopg@lists.postgresql.org; Tue, 21 Feb 2023 08:55:46 +0000 Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4PLY635Cvyz9sR6 for ; Tue, 21 Feb 2023 09:55:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aryehleib.com; s=MBO0001; t=1676969739; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=85rIO0RQtxIhfsQhApE9Kslq0DcdePlqqP0ZZkr3iNQ=; b=VXwxPuf44K1Ckpj0sG+SbNz/kdQu6LnpvJkO+ZwCJU12eNpA7pdJ/9QSexKnDfTAw4C+sU 9AzLmfNB0H66nIwnmFPxvPonuUO1btEwNA5K4avqqCBde1rSMPuJ33kwBE7NHYok+lfdkK vZotN9ccAUlQuLb5SGnRIPgyVPO0HUxHIEuiQGCNVwlFz99A0XGp0mitKG+Vdq94xtjZ08 qe5S/0PtqOfgSxXWwudR/6FzBsH+oxLkKXZCm9EUIAuMPzHl3s6YcDiJqikHZE4VTlI4r+ Ny9oBn1gBBmnahI+bN7mSk38jETz1MBA1ATH8nJJ7YobxeJX7stdVUDTiUadPA== Date: Tue, 21 Feb 2023 10:55:37 +0200 From: Aryeh Leib Taurog To: psycopg@lists.postgresql.org Subject: Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion? Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: x-markup: none List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk As Adrian points out, it is a bit of a strange use case, since the jsonb itself could be an array. If you really want to do this, however, have a look at pgcopy: https://pgcopy.readthedocs.io/ pgcopy makes COPY with psycopg2 easy, and supports jsonb[] (Note: I am the author) If you want to roll your own, feel free to have a look under the hood On Mon, Feb 20, 2023 at 11:34:19PM -0500, Samuel Marks wrote: > How do I insert into a table with a `json[]` column using the `COPY FROM` > syntax? > > Attempt: > https://gist.github.com/SamuelMarks/fec744a620e2abd0257671aa6f2a96b4 > > Error: > psycopg2.errors.InvalidTextRepresentation: malformed array literal: > "{"jj":null,"text":"bop"}" > > Or when I try to json.dump the dict twice I get: > psycopg2.errors.InvalidTextRepresentation: malformed array literal: > ""{"jj":null,"text":"bop"}"" DETAIL: Array value must start with "{" or > dimension information. CONTEXT: COPY my_table, line 1, column > json_arr_col: ""{"jj":null,"text":"bop"}"" > > I'm using your `copy_expert` function. PS: Also asked on > https://stackoverflow.com/q/75511919 > > Am I meant to represent the lists with braces rather than square brackets? > - Or should I be using some internal psycopg function as opposed to my > hacked together `parse_col` function? > > Thanks, > > Samuel Marks > Charity | consultancy > | open-source | LinkedIn >