Importing JSON into mysql using python -


im trying import mysql, have json output:

 {u'age': u'27',   u'foulscommitted': u'36',   u'foulssuffered': u'19',   u'goalassists': u'0',   u'no': u'18',   u'pla': u'jaume costa',   u'pos': u'd',   u'redcards': u'1',   u'shotsontarget': u'2',   u'starts': u'19',   u'subins': u'1',   u'totalgoals': u'0',   u'totalshots': u'7',   u'yellowcards': u'7'},  {u'age': u'28',   u'foulscommitted': u'0',   u'foulssuffered': u'0',   u'goalassists': u'0',   u'no': u'3',   u'pla': u'bojan jokic',   u'pos': u'd',   u'redcards': u'0',   u'shotsontarget': u'0',   u'starts': u'0',   u'subins': u'0',   u'totalgoals': u'0',   u'totalshots': u'0',   u'yellowcards': u'0'},  {u'age': u'18',   u'foulscommitted': u'4',   u'foulssuffered': u'2',   u'goalassists': u'0',   u'no': u'28',   u'pla': u'adri\xe1n mar\xedn ',   u'pos': u'd',   u'redcards': u'0',   u'shotsontarget': u'0',   u'starts': u'4',   u'subins': u'0',   u'totalgoals': u'0',   u'totalshots': u'1',   u'yellowcards': u'1'},  {u'age': u'24',   u'foulscommitted': u'10',   u'foulssuffered': u'10',   u'goalassists': u'0',   u'no': u'5',   u'pla': u'mateo musacchio',   u'pos': u'd',   u'redcards': u'0',   u'shotsontarget': u'4',   u'starts': u'10',   u'subins': u'1',   u'totalgoals': u'3',   u'totalshots': u'5',   u'yellowcards': u'1'},  {u'age': u'24',   u'foulscommitted': u'19',   u'foulssuffered': u'9',   u'goalassists': u'1',   u'no': u'2',   u'pla': u'mario gaspar ',   u'pos': u'd',   u'redcards': u'0',   u'shotsontarget': u'8',   u'starts': u'22',   u'subins': u'2',   u'totalgoals': u'3',   u'totalshots': u'24',   u'yellowcards': u'2'},  {u'age': u'27',   u'foulscommitted': u'15',   u'foulssuffered': u'9',   u'goalassists': u'2',   u'no': u'15',   u'pla': u'victor ruiz',   u'pos': u'd',   u'redcards': u'0',   u'shotsontarget': u'0',   u'starts': u'17',   u'subins': u'1',   u'totalgoals': u'0',   u'totalshots': u'3',   u'yellowcards': u'3'},  {u'age': u'30',   u'foulscommitted': u'11',   u'foulssuffered': u'9',   u'goalassists': u'0',   u'no': u'22',   u'pla': u'antonio rukavina',   u'pos': u'd',   u'redcards': u'0',   u'shotsontarget': u'0',   u'starts': u'11',   u'subins': u'3',   u'totalgoals': u'0',   u'totalshots': u'3',   u'yellowcards': u'3'},  {u'age': u'24',   u'foulscommitted': u'48',   u'foulssuffered': u'18',   u'goalassists': u'9',   u'no': u'17',   u'pla': u'denis cheryshev',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'20',   u'starts': u'17',   u'subins': u'7',   u'totalgoals': u'4',   u'totalshots': u'59',   u'yellowcards': u'6'},  {u'age': u'24',   u'foulscommitted': u'8',   u'foulssuffered': u'12',   u'goalassists': u'1',   u'no': u'6',   u'pla': u'jonathan dos santos',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'5',   u'starts': u'12',   u'subins': u'10',   u'totalgoals': u'2',   u'totalshots': u'10',   u'yellowcards': u'4'},  {u'age': u'20',   u'foulscommitted': u'24',   u'foulssuffered': u'13',   u'goalassists': u'4',   u'no': u'19',   u'pla': u'mois\xe9s g\xf3mez',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'11',   u'starts': u'16',   u'subins': u'8',   u'totalgoals': u'4',   u'totalshots': u'26',   u'yellowcards': u'5'},  {u'age': u'18',   u'foulscommitted': u'0',   u'foulssuffered': u'0',   u'goalassists': u'0',   u'no': u'27',   u'pla': u'nahuel leiva',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'0',   u'starts': u'0',   u'subins': u'0',   u'totalgoals': u'0',   u'totalshots': u'0',   u'yellowcards': u'0'},  {u'age': u'30',   u'foulscommitted': u'32',   u'foulssuffered': u'25',   u'goalassists': u'1',   u'no': u'21',   u'pla': u'bruno',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'7',   u'starts': u'20',   u'subins': u'1',   u'totalgoals': u'2',   u'totalshots': u'18',   u'yellowcards': u'3'},  {u'age': u'23',   u'foulscommitted': u'3',   u'foulssuffered': u'5',   u'goalassists': u'0',   u'no': u'26',   u'pla': u'sergio marcos',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'2',   u'starts': u'5',   u'subins': u'0',   u'totalgoals': u'0',   u'totalshots': u'3',   u'yellowcards': u'0'},  {u'age': u'27',   u'foulscommitted': u'26',   u'foulssuffered': u'18',   u'goalassists': u'3',   u'no': u'4',   u'pla': u'tom\xe1s pina',   u'pos': u'm',   u'redcards': u'1',   u'shotsontarget': u'1',   u'starts': u'10',   u'subins': u'5',   u'totalgoals': u'0',   u'totalshots': u'7',   u'yellowcards': u'3'},  {u'age': u'23',   u'foulscommitted': u'34',   u'foulssuffered': u'20',   u'goalassists': u'1',   u'no': u'14',   u'pla': u'manuel trigueros',   u'pos': u'm',   u'redcards': u'0',   u'shotsontarget': u'6',   u'starts': u'18',   u'subins': u'5',   u'totalgoals': u'1',   u'totalshots': u'22',   u'yellowcards': u'6'},  {u'age': u'22',   u'foulscommitted': u'5',   u'foulssuffered': u'14',   u'goalassists': u'0',   u'no': u'10',   u'pla': u'joel campbell',   u'pos': u'f',   u'redcards': u'0',   u'shotsontarget': u'3',   u'starts': u'7',   u'subins': u'0',   u'totalgoals': u'0',   u'totalshots': u'9',   u'yellowcards': u'1'},  {u'age': u'25',   u'foulscommitted': u'18',   u'foulssuffered': u'23',   u'goalassists': u'3',   u'no': u'9',   u'pla': u'giovani dos santos',   u'pos': u'f',   u'redcards': u'0',   u'shotsontarget': u'8',   u'starts': u'13',   u'subins': u'8',   u'totalgoals': u'1',   u'totalshots': u'35',   u'yellowcards': u'2'},  {u'age': u'22',   u'foulscommitted': u'15',   u'foulssuffered': u'15',   u'goalassists': u'3',   u'no': u'23',   u'pla': u'gerard moreno',   u'pos': u'f',   u'redcards': u'0',   u'shotsontarget': u'15',   u'starts': u'11',   u'subins': u'5',   u'totalgoals': u'5',   u'totalshots': u'26',   u'yellowcards': u'2'},  {u'age': u'31',   u'foulscommitted': u'28',   u'foulssuffered': u'31',   u'goalassists': u'1',   u'no': u'8',   u'pla': u'ikechukwu uche',   u'pos': u'f',   u'redcards': u'0',   u'shotsontarget': u'15',   u'starts': u'17',   u'subins': u'4',   u'totalgoals': u'5',   u'totalshots': u'37',   u'yellowcards': u'2'}]          import json         testfile = open("output.json")         data = json.load(testfile) 

i tried setting

pla = data["pla"] 

i following error: "list indices must integers, not str"

the following insert statement:

sql = "insert soccer_player (player_name, player_no , player_pos) values ( pla , no , pos)"   

what issue here?

according json data you've shown, data becomes list of dictionaries.

if want insert of items in list mysql database, can approach executemany():

sql = """     insert          soccer_player (player_name, player_no , player_pos)      values          (%(pla)s, %(no)s, %(pos)s) """  cursor.executemany(sql, data) 

note placeholders i'm using %(pla)s, %(no)s , %(pos)s - special syntax "keyword-parameterized" queries in called "python extended format" (pep-249).


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -