5

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))
1
  • Just an FYI. The syntax of the sample.json seems erroneous. I ll make an edit and reshape it asap Commented Dec 22, 2018 at 22:26

1 Answer 1

4

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))
Sign up to request clarification or add additional context in comments.

Comments

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.