将Python的dicts列表转换为json的Postgresql数组

时间:2022-10-18 18:08:37

I am trying to insert a Python(2.7) list of jsonb elements into a Postgresql(9.4) table with a column of datatype: jsonb[].

我试图将一个Python(2.7)jsonb元素列表插入到Postgresql(9.4)表中,其中包含一个数据类型列:jsonb []。

Here's some code:

这是一些代码:

import json
anArray = [{"name":"Joe","age":51,"yob":1964,"gender":"male"},{"name":"George","age":41,"dob":1974,"gender":"male"},{"name":"Nick","age":31,"dob":1984,"gender":"male"}]
myArray = []
#here's what I have so far: 
for e in anArray:
    myArray.append(json.dumps(e))
#this gives me
myArray = ['{"name":"Joe","age":51,"yob":1964,"gender":"male"}','{"name":"George","age":41,"dob":1974,"gender":"male"}','{"name":"Nick","age":31,"dob":1984,"gender":"male"}']
#insert commands
insert_sql = "INSERT INTO my_table (data) VALUES (%s);"
insert_data = (myArray, )
cursor.execute(insert_sql, insert_data)

Now when I try to insert myArray, psycopg2 gives me an error

现在当我尝试插入myArray时,psycopg2给了我一个错误

psycopg2.ProgrammingError: column "data" is of type jsonb[] but expression is of type text[]

I'm not quite sure what the correct syntax is to insert this data into the table. Any help/pointers will be appreciated.

我不太确定将这些数据插入表中的正确语法是什么。任何帮助/指针将不胜感激。

Solution Thanks to piro, it was a quick solution.

解决方案感谢piro,这是一个快速的解决方案。

insert_sql = "INSERT INTO my_table (columns) VALUES (%s::jsonb[]);"
insert_data = (myArray, )
cursor.execute(insert_sql, insert_data)

2 个解决方案

#1


5  

This is not a psycopg error: it is a PostgreSQL error psycopg is just relying.

这不是psycopg错误:它是一个PostgreSQL错误psycopg只是依赖。

The error seems suggesting there is no implicit text[]->jsonb[] cast so you need to add a manual one:

错误似乎表明没有隐式文本[] - > jsonb []强制转换,因此您需要添加一个手动文本:

INSERT INTO my_table (columns) VALUES (%s::jsonb[]);

#2


2  

Use Psycopg JSON adaptation

使用Psycopg JSON适配

from psycopg2.extras import Json

data = [
    {"name":"Joe","age":51,"yob":1964,"gender":"male"},
    {"name":"George","age":41,"dob":1974,"gender":"male"},
    {"name":"Nick","age":31,"dob":1984,"gender":"male"}
]

query = '''
    insert into t (j)
    select array_agg(j)
    from jsonb_array_elements(%s) s(j)
'''

cursor.execute(query, (Json(data),))

jsonb_array_elements will return a set of jsonb which will be turned into an array of jsonb by array_agg

jsonb_array_elements将返回一组jsonb,它将通过array_agg转换为jsonb数组

#1


5  

This is not a psycopg error: it is a PostgreSQL error psycopg is just relying.

这不是psycopg错误:它是一个PostgreSQL错误psycopg只是依赖。

The error seems suggesting there is no implicit text[]->jsonb[] cast so you need to add a manual one:

错误似乎表明没有隐式文本[] - > jsonb []强制转换,因此您需要添加一个手动文本:

INSERT INTO my_table (columns) VALUES (%s::jsonb[]);

#2


2  

Use Psycopg JSON adaptation

使用Psycopg JSON适配

from psycopg2.extras import Json

data = [
    {"name":"Joe","age":51,"yob":1964,"gender":"male"},
    {"name":"George","age":41,"dob":1974,"gender":"male"},
    {"name":"Nick","age":31,"dob":1984,"gender":"male"}
]

query = '''
    insert into t (j)
    select array_agg(j)
    from jsonb_array_elements(%s) s(j)
'''

cursor.execute(query, (Json(data),))

jsonb_array_elements will return a set of jsonb which will be turned into an array of jsonb by array_agg

jsonb_array_elements将返回一组jsonb,它将通过array_agg转换为jsonb数组