Импорт списка данных JSON в Postgresql с использованием Python - PullRequest
0 голосов
/ 04 июля 2018

Я пытаюсь импортировать список данных JSON в Postgresql, используя Python, но получаю эту ошибку

psycopg2.ProgrammingError: невозможно адаптировать тип 'dict'

когда я помещаю поле "custom_fields" с типом данных JSONB [] в мою созданную таблицу в базе данных Postgresql. Есть ли способ исправить мою проблему? Любая помощь будет делать. Большое вам спасибо

// sample.json

{
    "url": "https://www.abcd.com/",
    "id": 123456789,
    "external_id": null,
    "via": {
        "channel": "email",
        "id": 4, 
        "source": {
            "from": {
            "address": "abc@abc.com", 
            "name": "abc def"
        }, 
        "rel": null, 
        "to": {
            "address": "def@def.com", 
            "name": "def"
            }
        }
    },
    "custom_fields": [
        {
            "id": 234567891,
            "value": "abc_def_ghi"
        },
        {
            "id": 345678912, 
            "value": null
        },
        {
            "id": 456789123, 
            "value": null
        }
    ]
},

{
    "url": "http://wxyz.com/", 
    "id": 987654321, 
    "external_id": null, 
    "via": {
        "channel": "email",
        "id": 4,
        "source": {
            "from": {
                "address": "xyz@xyz.com", 
                "name": "uvw xyz"
            }, 
            "rel": null, 
            "to": {
                "address": "zxc@zxc.com", 
                "name": "zxc"
            }
        }
    },
    "custom_fields": [
        {
            "id": 876543219, 
            "value": "zxc_vbn_asd"
        }, 
        {
            "id": 765432198, 
            "value": null
        }, 
        {
            "id": 654321987, 
            "value": null
        }
    ]   
}

// my_code.py

import json
import psycopg2

connection = psycopg2.connect("host=localhost dbname=sample user=gerald password=1234")
cursor = connection.cursor()

data = []
with open('sample.json') as f:
    for row in f:
        data.append(json.loads(row))

fields = [
    'url',
    'id',
    'external_id',
    'via',
    'custom_fields'
]

for item in data:
    my_data = [item[field] for field in fields]
    for key, value in enumerate(my_data):
        if isinstance(value, dict):
            my_data[key] = json.dumps(value)
    insert_query = "INSERT INTO crm VALUES (%s, %s, %s, %s, %s) "
    cursor.execute(insert_query, tuple(my_data))

connection.commit()
connection.close()

1 Ответ

0 голосов
/ 04 июля 2018

Фреймворки, такие как SQLAlchemy, были бы моими идеями для подобных операций.

Полезная нагрузка JSON в приведенном выше примере является вложенной. Вы должны убедиться, что соответствующие целевые БД в вашей БД отражают ту же схему.

Пример БЮР:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_declarative import  Base, Review

engine = create_engine('sqlite:///sqlalchemy_try.db') #you might want to tweak this dialect to db of you choice. 
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)

session = DBSession()
with open('sample.json') as f:
    data=f.read()
    jsondata=json.loads(data)
    r = Review(jsondata['url'], int(jsondata['id']), jsondata['external_id'], jsondata['via']['channel']))
    session.add(r)
session.commit()
...