2

I am currently trying to store two lists of json objects into my postgresql database using psycopg2 without success.

Here's a snippet of what I am trying to do...

SQL_INSERT_QUERY = """INSERT INTO table (
                              x,
                              json_list_one,
                              json_list_two
                            ) VALUES (%s, %s, %s)"""

def insert_stuff(id, json_list_one, json_list_two):
values = (
    id,
    map(psycopg2.extras.Json, json_list_one),
    map(psycopg2.extras.Json, json_list_two)
)
conn = get_connection_pool().getconn()
try:
    cur = conn.cursor()
    cur.execute(SQL_INSERT_QUERY, values)
    conn.commit()
    cur.close()
finally:
    get_connection_pool().putconn(conn)

This current implementation is resulting in this error...

An exception of type ProgrammingError occured. Arguments:
('column "json_list_one" is of type json[] but expression is of type text[]\nLINE 5: ...) VALUES (\'9527d790-31dc-46fa-b683-c6fc9807c2a4\', ARRAY[\'{"h...\n                                                             ^\nHINT:  You will need to rewrite or cast the expression.\n',)

Does anyone know how I am supposed to insert an array of json objects in PostgreSQL?

Thank you.

1

3 Answers 3

2

You need cast value to son --

SQL_INSERT_QUERY = """INSERT INTO table (
                              x,
                              json_list_one,
                              json_list_two
                            ) VALUES (%s, %s::json[], %s::json[])"""

And if you use PostgreSQL 9.5 or later you need querying over json fields is better to change json to jsonb. Jsonb allow to create GIN index than allow increase performance of your query.

Sign up to request clarification or add additional context in comments.

2 Comments

This returns DETAIL: Array value must start with "{" or dimension information . what could be wrong?
By default postgres cast unknown data as text. As you can see in error 'column "json_list_one" is of type json[] but expression is of type text[] than error in bad type for field - text[] instead json[].
0

from here: https://stackoverflow.com/a/45150668/3598837

from psycopg2.extras import Json

cur = conn.cursor()

cur.execute('INSERT into some_table (uuid, dict) values (%s, %s)',
    ['testName', Json([{'name':'test','number':'444-444-4444'}, {'name':'test','number':'555-555-5555'}])])
#the rest of it

Comments

-4

You need to use the json library and call json.dumps(dict_here). Json is not a supported type in Postgres. You use json.dumps to turn a dictionary into a json string, and json.loads to turn it back into a dictionary.

1 Comment

Actually PosgreSQL does support Json type (since 9.3?). I have stored Json (not as an array) in other tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.