title: Importing JSON Object into Postgresql using Python
body: <p>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.</p>
  
  <p><strong>sample.json</strong></p>
  
  <pre><code>[
    {
      "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"
          }
        }
      }
    }
  ]
  </code></pre>
  
  <p><strong>my_code.py</strong></p>
  
  <pre><code>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))
  </code></pre>