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 1pUL3W-0000G1-07 for psycopg@arkaria.postgresql.org; Tue, 21 Feb 2023 05:19:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUL3U-0007uI-4S for psycopg@arkaria.postgresql.org; Tue, 21 Feb 2023 05:19:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUL3S-0007t3-EB for psycopg@lists.postgresql.org; Tue, 21 Feb 2023 05:19:07 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUL3M-0003kr-Mc for psycopg@lists.postgresql.org; Tue, 21 Feb 2023 05:19:05 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 6390A5C020F; Tue, 21 Feb 2023 00:18:56 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 21 Feb 2023 00:18:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1676956736; x= 1677043136; bh=vrl1/lgl+iC/dHJ2JvSNsiBlZCHg5DdTVJp86FeoUDo=; b=L tjHHW2FUYL4N6zeHudf1WLjvGsDNe6UwGnCu5Q2rnpOMeQrvWOMt6MkIf+m9Kg5p RXJW7uJxh9n0NR6kHMWRsqyVvy+W89B0M0gyFzd6xZzsNXyPRSwzRrCoX/v2MsuE Y/lkSA2wx+JaP7jAuyiCUG16OpAuCaR42zOEDGRFcesROb1kClSUdyoGD4VfObiY Cmf0Ca9nVhwbg7ooNuMAapNlp/9eQN0aiVjHWQxmoLj6ecGVBy9djmmkqST5Vh7C r/nBKYrqQCKx1bgoFwL4vRsB0rZKfR+nUcNtxObElvMxQGF4yrAD4u+2qIzUwTg6 PiPZw1ljGcw6U76ToqxPg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1676956736; x=1677043136; bh=v rl1/lgl+iC/dHJ2JvSNsiBlZCHg5DdTVJp86FeoUDo=; b=QDiBX4pUK28z+ifgF Sh5W8pMuf4SnT2HTxN4TpvE6uaOLS3IMa6BjmKFaFA8UQdbJcOEQz5VPgvRkH+LY 4FOpLuOE2FlKXPyPgg5vEv02Kh8JFjsOUezvo/XGU2pc5MMK85WblwM+5vQ/Koxa CvQKskBLYBuuO+CLDhuQLsynoUJ7g2SOckJpL7irUGCRxikOO6IDn8EOCzct7nmp e5n5CpERqlUYJ+8ldUs5nHAjiVT4ArMqQaaP17rRidoPbNkUBmenbihhvzBq7Zcs WC4gh6UEtkaXp3qofPbE58JZitZdOlQy3Wby1xjifzdLv7FIA7y8F+bIBFDlIXHB I/jfA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudejiedgkeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeeugfegieehveduhfevgedukeekvdfgtddtgeevffei hedvtdffgeekkefhieehhfenucffohhmrghinhepghhithhhuhgsrdgtohhmpdhsthgrtg hkohhvvghrfhhlohifrdgtohhmpdhshigunhgvhihstghivghnthhifhhitgdrohhrghdp ohhffhhstggrlhgvrdhiohdplhhinhhkvgguihhnrdgtohhmnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhes rghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 21 Feb 2023 00:18:55 -0500 (EST) Message-ID: <7c40b067-0bf6-82f9-4b88-9a95242d3811@aklaver.com> Date: Mon, 20 Feb 2023 21:18:54 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion? Content-Language: en-US To: Samuel Marks , psycopg@lists.postgresql.org References: From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/20/23 20:34, Samuel Marks wrote: > How do I insert into a table with a `json[]` column using the `COPY > FROM` syntax? Again why do you want to use json[]? What possible use is that, when you have json and jsonb types available? > > 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 -- Adrian Klaver adrian.klaver@aklaver.com