Insert Google Analytics API data to postgresql [python]
Question: I want to store bulk data to postgresql. The data I got are from google
analytics [API]. The data is about pageviews and here is my code:

    
    
    data = '[["20151201","path","title",345], ["20151202","path","title",321], ["20151203","path","title",214]]'
    
    def storeJson( jsonFile, tableName ):
        conn = psycopg2.connect( host=hostname, user=username, password=password, dbname=database )
        try:
            cur = conn.cursor()
            # Here is the problem:
            cur.executemany( "INSERT INTO " + tableName + " VALUES(%s)", [jsonFile])
            conn.commit()
    
        except psycopg2.DatabaseError as e:
            if conn:
                conn.rollback()
            print("Error %s" %e)
            exit()
    
        finally:
            if conn:
                cur.close()
                conn.close()
    
    def main()
        storeJson(data, "daily_pageviews")
    
    if __name__ == '__main__':
        main()
    

with the code above, i got error message like this:

> json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 12
> (char 11)

Can someone enlighten me? Thanks guys!


Answer: _`jsonFile` is a string_ in your case. You need to load it with
`json.loads()`:

    
    
    import json
    
    data = json.loads(jsonFile)
    cur.executemany("INSERT INTO " + tableName + " VALUES(%s, %s, %s, %s)", data)
    

Note that I have _4 placeholders_ in the query - each for every item in every
sublist.

