1
import requests

response = requests.get("https://api.exchangeratesapi.io/latest?base=USD")
print(response.json())

gets me

{"rates":{"CAD":1.327455236,"HKD":7.8246518358,"ISK":123.259178875,"PHP":50.8545849159,"DKK":6.7577319588,"HUF":302.3060227889,"CZK":23.0719840839,"GBP":0.7775366251,"RON":4.3173268222,"SEK":9.6097847712,"IDR":14084.997287032,"INR":71.6865617652,"BRL":4.188189546,"RUB":63.6969614759,"HRK":6.7249954784,"JPY":108.545849159,"THB":30.1953336951,"CHF":0.994212335,"EUR":0.9043226623,"MYR":4.1710074154,"BGN":1.768674263,"TRY":5.7079942123,"CNY":7.0387050099,"NOK":9.1411647676,"NZD":1.5586905408,"ZAR":14.7149574968,"USD":1.0,"MXN":19.3951890034,"SGD":1.3629951167,"AUD":1.4728703201,"ILS":3.4664496292,"KRW":1178.395731597,"PLN":3.8865075059},"base":"USD","date":"2019-11-22"}

How do I assign the EUR (for example) int to a variable? I've tried a few different ways and can't get it.

1 Answer 1

4

You'd do

eur = response.json()['rates']['EUR']

You address JSON objects like dictionaries since python does decode them into lists and dictionaries accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't address JSON objects (which are all strings) at all; you use the Python object that response.json() returns.
Yeah, I mentioned that it gets decoded into lists and dictionaries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.