I am trying to insert some JSON files in PostgreSQL, but I am kind of new in it and I have some questions. So, I have the files already, which are encoded and here is a simple example of what I am trying to do. Creating some tables in Postgres and adding twitter information in it.
import json
import psycopg2
conn = psycopg2.connect("dbname='postgres' user='bilabial' host='localhost'")
cur = conn.cursor()
linenumber = 0
data = open('tweets_testing.json').read()
for line in data:
linenumber += 1
try:
str_line = line.strip()
vals = json.loads(str_line)
tweets = vals["text"]
cur.execute("CREATE TABLE tweetstext (text VARCHAR(255));")
cur.execute("INSERT INTO tweetstext (" + tweets+ ");")
conn.commit()
except KeyError:
break
So the error I get is "TypeError: list indices must be integers, not str". I don't understand very much the difference of it when I read about it, so any help would be really appreciated.