0

How to extract data of specific district (e.g. Nicobars, North and Middle Andaman...) from https://api.covid19india.org/state_district_wise.json I need to extract Active just by searching for the district name. Right now I'm extracting it this way:

url='https://api.covid19india.org/state_district_wise.json'
r = urllib.request.urlopen(url)
data = r.read().decode()
js=json.loads(data)
print('Confirmed cases:', js[State_name]['districtData']["District_name"]['confirmed'])

I don't want to specify state name every time.

1
  • You should edit the question to be more clear on what you want to do. Commented Sep 13, 2020 at 21:45

1 Answer 1

2
url= 'https://api.covid19india.org/state_district_wise.json'  
r = urllib.request.urlopen(url)  
data = r.read().decode()  
js = json.loads(data)  

district = "Prakasam" 

for state, value in js.items():  
    if not district in value['districtData'].keys(): 
        continue 
      
    value = value['districtData'][district] 

    print(f"State: {state}") 
    print(f"* District: {district}")  
    print(f"** Active: {value['active']}")  
    print(f"** Confirmed: {value['confirmed']}")
Sign up to request clarification or add additional context in comments.

3 Comments

This is returning all the district values, I want to search for a specific district by its name
This works. If you don't mind can you explain what's actually happening here, I didn't understand
I just iterate through json keys (states, then districts) and skip districts that don’t match ‘district’ variable’s value

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.