Re: copying json data and backslashes

Поиск
Список
Период
Сортировка
От pbj@cmicdo.com
Тема Re: copying json data and backslashes
Дата
Msg-id 1999020343.1943388.1669131046107@mail.yahoo.com
обсуждение исходный текст
Ответ на Re: copying json data and backslashes  (Erik Wienhold <ewie@ewie.name>)
Ответы Re: copying json data and backslashes  (Alastair McKinley <a.mckinley@analyticsengines.com>)
Список pgsql-general
 >
 > On Tuesday, November 22, 2022 at 10:16:11 AM EST, Erik Wienhold <ewie@ewie.name> wrote:
 >
 >
 > > On 22/11/2022 15:23 CET Alastair McKinley <a.mckinley@analyticsengines.com> wrote:
 > >
 > > Hi all,
 > >
 > > I have come across this apparently common issue COPY-ing json and wondering if
 > > there is potentially a better solution.
 > >
 > > I am copying data into a jsonb column originating from a 3rd party API. The
 > > data may have literal \r,\t,\n and also double backslashes.
 > >
 > > I discovered that I can cast this data to a jsonb value directly but I can't
 > > COPY the data without pre-processing.
 >
 >
 > > Is there any other solution with COPY that doesn't require manual
 > > implementation of search/replace to handle these edge cases?
 > > Why does ::jsonb work but COPY doesn't? It seems a bit inconsistent.
 >
 > COPY handles special backslash sequences[1].  The \r in your sample JSON,
 > although properly escaped according to JSON, is replaced with an actual
 > carriage return by COPY before casting to jsonb.  The error results from JSON
 > prohibiting unescaped control characters in strings[2].
 >
 > You must double escape to pass those characters through COPY.
 >
 > See how COPY outputs backslash sequences:
 >
 >     -- Actual carriage return:
 >     copy (select e'\r') to stdout;
 >     \r
 >
 >     -- Backslash sequence for carriage return:
 >     copy (select '\r') to stdout;
 >
 >     \\r




В списке pgsql-general по дате отправления:

Предыдущее
От: Erik Wienhold
Дата:
Сообщение: Re: copying json data and backslashes
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: system variable can be edited by all user?