how to insert specific json into sqlite database using python 3 -
using python 3, want download api data, returned json, , want insert specific (columns or fields or whatever?) sqlite database. so, here's i've got , issues have:
using python's request module:
##### import modules import sqlite3 import requests import json headers = { 'authorization' : 'ujbosdlknfsodiflksdonosb4aa=', 'accept' : 'application/json' } r = requests.get( 'https://api.lendingclub.com/api/investor/v1/accounts/94837758/detailednotes', headers=headers )
okay, first issue how requested json data (a dictionary?) python can use. that...
jason.loads(r.text)
then create table want insert specific data:
curs.execute('''create table data( loanid integer not null, noteamount real not null, )''')
no problem there...but now, though json data looks (although there hundreds of records)...
{ "mynotes": [ { "loanid":11111, "noteid":22222, "orderid":33333, "purpose":"debt consolidation", "canbetraded":true, "credittrend":"down", "loanamount":10800, "noteamount":25, "paymentsreceived":5.88, "accruedinterest":12.1, "principalpending":20.94, }, { "loanid":11111, "noteid":22222, "orderid":33333, "purpose":"credit card refinancing", "canbetraded":true, "credittrend":"up", "loanamount":3000, "noteamount":25, "paymentsreceived":7.65, "accruedinterest":11.92, "principalpending":19.76, }] }
i want insert 2 data points sqlite database, "loanid" , "noteamount". believe inserting data database (but know incorrect):
curs.execute('insert data (loanid, noteamount) values (?,?)', (loanid, noteamount))
but @ total loss how that, guess have 2 main issues; getting downloaded data python can use insert specific data database; , how insert data database object holds downloaded data. i'm guessing looping part of answer...but what? in advance!
as the documentation says:
the sqlite3 module supports 2 kinds of placeholders: question marks (qmark style) , named placeholders (named style).
note can insert rows @ once using executemany
. in case:
curs.executemany('insert data (loanid, noteamount) ' 'values (:loanid,:noteamount)', json.loads(...)['mynotes'])
Comments
Post a Comment