Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing JSON Object into Postgresql using Python

Tags:

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": "[email protected]",
          "name": "abc def"
        },
        "rel": null,
        "to": {
          "address": "[email protected]",
          "name": "def"
        }
      }
    }
  },
  {
    "url": "http://wxyz.com/",
    "id": 987654321,
    "external_id": null,
    "via": {
      "channel": "email",
      "id": 4,
      "source": {
        "from": {
          "address": "[email protected]",
          "name": "uvw xyz"
        },
        "rel": null,
        "to": {
          "address": "[email protected]",
          "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))
like image 513
Gerald Milan Avatar asked Jul 03 '18 02:07

Gerald Milan


1 Answers

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))
like image 174
Waket Zheng Avatar answered Sep 28 '22 19:09

Waket Zheng