0

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.

Nasia Ntalla
  • 1,059
  • 3
  • 11
  • 25
  • Can you share an example `str_line` ? – Mureinik Oct 05 '15 at 12:50
  • could you please post the full error? – Azsgy Oct 05 '15 at 12:50
  • File "SQLpy.py", line 18, in tweets = vals["text"] TypeError: list indices must be integers, not str – Nasia Ntalla Oct 05 '15 at 12:51
  • The line number is not 18, as I have written the code here. It is about the line saying "tweets=vals["text"]" – Nasia Ntalla Oct 05 '15 at 12:53
  • Also, you have an SQL injection vulnerability in your code. see http://stackoverflow.com/questions/601300/what-is-sql-injection and http://stackoverflow.com/questions/1973/what-is-the-best-way-to-avoid-sql-injection-attacks/2040#2040 – Azsgy Oct 05 '15 at 12:53

1 Answers1

1
vals = json.loads(str_line)

type of vals is list. Check the type of vals by print "Type vals", type(vals)

Demo : According to your code.

>>> import json
>>> str_line = "[1,2,3,5]"
>>> vals = json.loads(str_line)
>>> vals
[1, 2, 3, 5]
>>> type(vals)
<type 'list'>
>>> vals["text"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> 

Following line expecting type of valsshould be dict i.e. dictionary.

`tweets = vals["text"]`

Can you shear the value structure of str_line variable?


[Edit 2]

According to me we need to loads all content of json file.

data = open('tweets_testing.json').read()
vals = json.loads(data)

but code will change according to Input and Logic.

Vivek Sable
  • 8,494
  • 3
  • 32
  • 45
  • So I get it, str_line is a string and vals becomes a list, whereas it should be a dictionary. But I don't understand how I make it. Should I split the words on my own? – Nasia Ntalla Oct 05 '15 at 13:03
  • can you shear sample input json file with us ? – Vivek Sable Oct 05 '15 at 13:04
  • [{ "contributors": null, "coordinates": null, "created_at": "Fri Sep 25 12:43:12 +0000 2015", "entities": { "hashtags": [ { "indices": [ 128, 139 ], "text": "vegetarian" } ], "symbols": [], "urls": [ ......] – Nasia Ntalla Oct 05 '15 at 13:07
  • you got solution? We need to add `text` value to table. but from the given json I not able to get. Can you pass me json file vivekbsable@gmail.com ? – Vivek Sable Oct 05 '15 at 13:15
  • It doesn't allow me to post more than those characters. I will send it to you now! – Nasia Ntalla Oct 05 '15 at 13:21
  • `"text": "This pretty green side salad may add sparkle to your dinner party? Cucumber Hearts & Candied Pecans https://t.co/fgdJXM4CHy #vegetarian"` This is tweet? – Vivek Sable Oct 05 '15 at 13:35