Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing and printing JSON result after GET request using Python

I try to make a clear result of a json result after a GET request on Python :

import requests
import json

r = requests.get("https://smspva.com/api/rent.php?method=getcountries")

parsed = json.loads(r.text)

print(parsed)

I got result like that :

{'status': 1, 'data': [{'name': 'Russian Federation', 'code': 'RU'}, {'name': 'Ukraine', 'code': 'UA'}, {'name': 'Germany', 'code': 'DE'}, {'name': 'Czech Republic', 'code': 'CZ'}, {'name': 'United Kingdom', 'code': 'UK'}, {'name': 'Sweden', 'code': 'SE'}, {'name': 'Spain', 'code': 'ES'}, {'name': 'Portugal', 'code': 'PT'}, {'name': 'Netherlands', 'code': 'NL'}, {'name': 'Lithuania ', 'code': 'LT'}, {'name': 'Latvia', 'code': 'LV'}, {'name': 'Ireland', 'code': 'IE'}, {'name': 'Estonia', 'code': 'EE'}, {'name': 'United States', 'code': 'US'}]}

How can i get something like :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Name : Russian Federation
Code : RU

Name : Ukraine
Code : Ua

etc etc

Thanks for your help !

>Solution :

You can use Response.json(). Then iterate over data key:

import requests

resp = requests.get("https://smspva.com/api/rent.php?method=getcountries")
data = resp.json()
for country in data.get('data', []):
    print(f"Name:{country.get('name')} Code:{country.get('code')}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading