I have this code sending JSON data from a URL to a MySQL database however I get the error 'String indices must be integers'
import urllib.parse
import requests
import pymysql
mydb = pymysql.connect(host='127.0.0.1', user='test', passwd='test', db='test', local_infile=1)
r = requests.get('https://example.com/apidata')
cursor = mydb.cursor()
json_obj = r.json()
for ord in json_obj["0"]:
print("stripe_token", ord["stripe_token"])
cursor.execute("INSERT INTO apidata (stripe_token) VALUES (%s)", (ord["stripe_token"]))
#close the connection to the database.
mydb.commit()
cursor.close()
My Json data looks like this
{
"0": {
"need_session_refresh": "1",
"signup_email_sent": "1",
"stripe_cc_expires": "0000",
"stripe_cc_masked": "123456",
"stripe_token": "1x734g834",
What am I doing wrong?
Thanks :)
Edit - I'd like to also parse this recursively e.g
{
"0": {
"stripe_token": "756474745",
"need_session_refresh": "1",
},
"1": {
"nstripe_token": "34563456",
"need_session_refresh": "1",
},
"_total": 43054
}