How to fetch JSON via URL and parse in Python
In this post, I am going to explain simple a code snippet that is very helpful for lots of applications. With Python, we are going to fetch JSON content from the URL and parse some fields
We will use Python as a programming language. In Python, to request the content of the URL, you can use the requests library. Let’s test the following code snippet
response = requests.get(‘https://api.exchangerate.host/latest')
data = response.json()
print(data)
This code snippet prints the following output
{
“motd”:{
“msg”:”If you or your company use this project or like what we doing, please consider backing us so we can continue maintaining and evolving this project.”,
“url”:”https://exchangerate.host/#/donate"
},
“success”:true,
“base”:”EUR”,
“date”:”2023–09–01",
“rates”:{
“AED”:3.980607,
“AFN”:88.275247,
“ALL”:108.652558,
“AMD”:419.641174,
As a next step, what we want to do is to parse the data and show the USD exchange rate
json library allows to show the specific content of the JSON
print(data[“rates”][“USD”])
It shows the USD/EUR rates: 1.084458
Conclusion
In this blog, we have implemented a sample application that fetches JSON via URL. As you see, it is a very simple approach with Python applications.