Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Samuel Marks <samuelmarks(at)gmail(dot)com>, psycopg(at)lists(dot)postgresql(dot)org
Subject: Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?
Date: 2023-02-21 05:18:54
Message-ID: 7c40b067-0bf6-82f9-4b88-9a95242d3811@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

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
> <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 <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 <https://sydneyscientific.org> | consultancy
> <https://offscale.io> | open-source <https://github.com/offscale> |
> LinkedIn <https://linkedin.com/in/samuelmarks>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse psycopg by date

  From Date Subject
Next Message Aryeh Leib Taurog 2023-02-21 08:55:37 Re: Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?
Previous Message Samuel Marks 2023-02-21 04:34:19 Escape Python to `json[]` for `COPY FROM` PostgreSQL insertion?