简体   繁体   中英

Importing JSON Object into Postgresql using Python

I'm trying to import JSON Data into the Postgresql using Python but, I'm getting "psycopg2.ProgrammingError: can't adapt type 'dict'" error when I put the "via" field with a JSONB data type in my created table in the Postgresql Database. Is there any way to fix my problem? Any help would do.

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"
        }
      }
    }
  },
  {
    "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"
        }
      }
    }
  }
]

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 line in f:
        data.append(json.loads(line))

fields = [
    'url', #varchar
    'id', #BigInt
    'external_id', #BigInt Nullable
    'via' #JSONB
]

for item in data:
    my_data = [item[field] for field in fields]
    insert_query = "INSERT INTO crm VALUES (%s, %s, %s, %s)"
    cursor.execute(insert_query, tuple(my_data))

One solution is dumps the dict before insert to db:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM