# W tej wersji dane JSON mamy dostępne jako tekst
# Jak zrobić z tego listę/słownik, jak potraktować jako JSON?

# Odp: użyjemy modułu json z biblioteki standardowej
# Uwaga: To zadziała dla dowolnego JSONa, także zawierającego false lub null.

import json

dane_tekstowe = '[{"table":"A","no":"237/A/NBP/2020","effectiveDate":"2020-12-04","rates":[{"currency":"bat (Tajlandia)","code":"THB","mid":0.1219},{"currency":"dolar amerykański","code":"USD","mid":3.6765},{"currency":"dolar australijski","code":"AUD","mid":2.7305},{"currency":"dolar Hongkongu","code":"HKD","mid":0.4743},{"currency":"dolar kanadyjski","code":"CAD","mid":2.8596},{"currency":"dolar nowozelandzki","code":"NZD","mid":2.5928},{"currency":"dolar singapurski","code":"SGD","mid":2.7600},{"currency":"euro","code":"EUR","mid":4.4732},{"currency":"forint (Węgry)","code":"HUF","mid":0.012474},{"currency":"frank szwajcarski","code":"CHF","mid":4.1241},{"currency":"funt szterling","code":"GBP","mid":4.9565},{"currency":"hrywna (Ukraina)","code":"UAH","mid":0.1300},{"currency":"jen (Japonia)","code":"JPY","mid":0.035367},{"currency":"korona czeska","code":"CZK","mid":0.1689},{"currency":"korona duńska","code":"DKK","mid":0.6009},{"currency":"korona islandzka","code":"ISK","mid":0.029294},{"currency":"korona norweska","code":"NOK","mid":0.4198},{"currency":"korona szwedzka","code":"SEK","mid":0.4356},{"currency":"kuna (Chorwacja)","code":"HRK","mid":0.5928},{"currency":"lej rumuński","code":"RON","mid":0.9179},{"currency":"lew (Bułgaria)","code":"BGN","mid":2.2871},{"currency":"lira turecka","code":"TRY","mid":0.4734},{"currency":"nowy izraelski szekel","code":"ILS","mid":1.1257},{"currency":"peso chilijskie","code":"CLP","mid":0.004898},{"currency":"peso filipińskie","code":"PHP","mid":0.0765},{"currency":"peso meksykańskie","code":"MXN","mid":0.1851},{"currency":"rand (Republika Południowej Afryki)","code":"ZAR","mid":0.2420},{"currency":"real (Brazylia)","code":"BRL","mid":0.7136},{"currency":"ringgit (Malezja)","code":"MYR","mid":0.9055},{"currency":"rubel rosyjski","code":"RUB","mid":0.0495},{"currency":"rupia indonezyjska","code":"IDR","mid":0.00026065},{"currency":"rupia indyjska","code":"INR","mid":0.049835},{"currency":"won południowokoreański","code":"KRW","mid":0.003392},{"currency":"yuan renminbi (Chiny)","code":"CNY","mid":0.5630},{"currency":"SDR (MFW)","code":"XDR","mid":5.2891}]}]'

dane = json.loads(dane_tekstowe)

print(type(dane))
print(dane)

slownik = dane[0]
print(type(slownik))
print(slownik)
print()

print('Tabela nr', slownik["no"], 'z dnia', slownik["effectiveDate"])

rates = slownik["rates"]
print('Liczba walut:', len(rates))
print()

for rate in rates:
    print(f'{rate["currency"]:36} ({rate["code"]}) → {rate["mid"]:12.9f}')
