We have a python script which pulls data form an API endpoint this way:
import urllib, json
url = "http://api.exchangeratesapi.io/v1/latest?access_key={test}"
response = urllib.urlopen(url)
record_list = json.loads(response.read())
We got to this point and we do see the data in record_list, however we don't know how to parse the data and insert it into a table.
We tried:
if type(record_list) == list:
first_record = record_list[0]
but it seems it is not a list, so what it is and how to import it into the DB?
The table has only 2 fields (currency_name and rate)
record_list sample:
{"success":true,"timestamp":1619448844,"base":"EUR","date":"2021-04-26","rates":{"AED":4.438765,"AFN":93.597683,"ALL":123.266852,"AMD":628.973606,"ANG":2.169505,"AOA":793.273462,"ARS":112.71963,"AUD":1.547521,"AWG":2.173782,"AZN":2.056227,"BAM":1.954933,"BBD":2.440381,"BDT":102.484579,"BGN":1.956421,"BHD":0.455567,"BIF":2377.111091,"BMD":1.208496,"BND":1.602106,"BOB":8.333368,"BRL":6.583276,"BSD":1.208646,"BTC":2.2483098e-5,"BTN":90.32643,"BWP":13.045517}}
Thanks!
record_listis, you could try printing it. If your question is actually about postgres, please include some code showing what you've done so far with postgres.record_list. Also what do you want to do with the data, insert it into a singleJSON(B)type field or insert across multiple fields? Edit your question to indicate the previous as well as include the schema for the table.JSONobject string and got converted byjson.loadsinto Python dictionary. You can the rates by doingrecord_list["rates"]which will yield a dictionary of currency/rate values. You can iterate over that to get the values to insert into the table. It would help if you edit your question to show the exact data you want to pull out of the dictionary and into which field the values should go.record_listis actuallyJSON. See my answer below to see how to deal with this.