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 1pUKMQ-0006gN-4H for psycopg@arkaria.postgresql.org; Tue, 21 Feb 2023 04:34:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUKMO-0003Ly-0W for psycopg@arkaria.postgresql.org; Tue, 21 Feb 2023 04:34:36 +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 1pUKMN-0003Ky-NS for psycopg@lists.postgresql.org; Tue, 21 Feb 2023 04:34:35 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pUKML-0005d8-B6 for psycopg@lists.postgresql.org; Tue, 21 Feb 2023 04:34:34 +0000 Received: by mail-ed1-x52e.google.com with SMTP id cq23so11887510edb.1 for ; Mon, 20 Feb 2023 20:34:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=VBiqE8ayCgsSTOvTU0HMNnTVIznCdyG7x9BDzee7oIc=; b=e+B9ijvE6lKYs3oiopzy8Bbl5nzC1DsLSjtu9iYp6pLSVa/7vEOx9FhyZuLPn/8HW8 vfX5Qua5d2tK3jzzUXOf+YUEzqwvnKxKFqj+YT+JQcL5V3qyNruko6mwjRiMDHvT99c4 oIcI+TMyc4TVblnW9k018aGsDwlEsO1kH6M2nLl0Ps2H/gBuO55S7gq2yGxfr+i8Y1Xp vC84BXC3Nv5CRo8gNfjvaI27XX1vSUfjJ66if77tgJpf3NfeIsYQMUgVcmsBsiSbA9Cd AsC0AYvLHIQybicykOxZ2ube08kj8eT2uIxHCP+YVATtshtyId8g2ibBqrrakB9oZzyP 65gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=VBiqE8ayCgsSTOvTU0HMNnTVIznCdyG7x9BDzee7oIc=; b=u9S/kZwHft3rsPPLn5EiyPspWVODBJ5fw4XcTZrbv96CRru3TIT8fpKkO/oWiQaYfr fO3+pFOB+gOj+rmQbIXsgsslQBb5ShqsRW2pDSB+krM/Op2b3x6tmaymw4Izsi446LAK pXiJew12HfAo3+VA/BQGU6wVuDwtq/8IvEb920LSf2MMYFuqiC+Bcu1boC95QhfURZ+X Gk3AJu4p/3G3Y2c1pyEHKNAQSUcs8nVJ/Hy3/gDfHqVP27SOSw4MVWaKT2l7+L3lBPAK nF+W9n0TvnNuY0lNchlcDktDx7PSk7X+taZe8Wm9rgrNIWyoivizeKR93ovb4K4M9Jz+ jgIQ== X-Gm-Message-State: AO0yUKWqgve+BzMVcjfniZ4b2m9FI7VHJapLBrklBul+JsgFjhIaXdDG X1uUY1ydzl/gnajgwwa3wayTGWuXnr+xIF6btmtfQAqU2uc= X-Google-Smtp-Source: AK7set8Ly2M1ZtYS5ZEHR9JELaNUrosvSG+5oLbqikJOxeat4Z/tc6c4D438XkurM5PxbqK4uCrwHJ0XTLTdfEW3BYE= X-Received: by 2002:a17:906:119b:b0:8d1:57cf:ef3c with SMTP id n27-20020a170906119b00b008d157cfef3cmr2490968eja.2.1676954071256; Mon, 20 Feb 2023 20:34:31 -0800 (PST) MIME-Version: 1.0 From: Samuel Marks Date: Mon, 20 Feb 2023 23:34:19 -0500 Message-ID: Subject: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion? To: psycopg@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000b8d46905f52e4b00" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000b8d46905f52e4b00 Content-Type: text/plain; charset="UTF-8" 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 --000000000000b8d46905f52e4b00 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
How do I insert into a table with a `json[]` column u= sing the `COPY FROM` syntax?

Attempt: h= ttps://gist.github.com/SamuelMarks/fec744a620e2abd0257671aa6f2a96b4

Error:
psycopg2.errors.InvalidTextRepresentation= : malformed array literal: "{"jj":null,"text":&quo= t;bop"}"

= Or when I try to json.dump the dict twice I get:
psycopg2.errors.Invalid= TextRepresentation: malformed array literal: ""{"jj":nu= ll,"text":"bop"}"" DETAIL: =C2=A0Array value = must start with "{" or dimension information. CONTEXT: =C2=A0COPY= my_table, line 1, column json_arr_col: ""{"jj":null,&q= uot;text":"bop"}""

I'm using your `copy_expert` function. PS: Also asked on https://stackoverflow.com/q/75511= 919

Am I meant to represent the lists with bra= ces rather than square brackets? - Or should I be using some internal psyco= pg function as opposed to my hacked together `parse_col` function?

Thanks,

Samuel Marks
--000000000000b8d46905f52e4b00--