-2

I have this data in .json file

{
    "sessions": [
        {
            "address": "Campbellbay",
            "available_capacity": 50,
            "available_capacity_dose1": 25,
            "available_capacity_dose2": 25,
            "block_name": "Campbell Bay",
            "center_id": 552109,
            "date": "19-05-2021",
            "district_name": "Nicobar",
            "fee": "0",
            "fee_type": "Free",
            "from": "09:00:00",
            "lat": 7,
            "long": 93,
            "min_age_limit": 45,
            "name": "Campbellbay PHC",
            "pincode": 744302,
            "session_id": "bfea957c-a34e-4b5f-99a0-4c739102eec5",
            "slots": [
                "09:00AM-11:00AM",
                "11:00AM-01:00PM",
                "01:00PM-03:00PM",
                "03:00PM-05:00PM"
            ],
            "state_name": "Andaman and Nicobar Islands",
            "to": "17:00:00",
            "vaccine": "COVISHIELD"
        },
        {
            "address": "Car Nicobar",
            "available_capacity": 45,
            "available_capacity_dose1": 20,
            "available_capacity_dose2": 25,
            "block_name": "Car Nicobar",
            "center_id": 570779,
            "date": "19-05-2021",
            "district_name": "Nicobar",
            "fee": "0",
            "fee_type": "Free",
            "from": "09:00:00",
            "lat": 9,
            "long": 92,
            "min_age_limit": 45,
            "name": "BJR Hospital",
            "pincode": 744301,
            "session_id": "f7d34d28-d936-4a25-95eb-7a1cdd09c545",
            "slots": [
                "09:00AM-11:00AM",
                "11:00AM-01:00PM",
                "01:00PM-03:00PM",
                "03:00PM-05:00PM"
            ],
            "state_name": "Andaman and Nicobar Islands",
            "to": "17:00:00",
            "vaccine": "COVISHIELD"
        },
        {
            "address": "Nancowry",
            "available_capacity": 50,
            "available_capacity_dose1": 25,
            "available_capacity_dose2": 25,
            "block_name": "Nancowry",
            "center_id": 552108,
            "date": "19-05-2021",
            "district_name": "Nicobar",
            "fee": "0",
            "fee_type": "Free",
            "from": "09:00:00",
            "lat": 7,
            "long": 93,
            "min_age_limit": 45,
            "name": "Nancowry CHC",
            "pincode": 744303,
            "session_id": "2a73f5c0-d0f8-4272-9cf1-2e68f20d8751",
            "slots": [
                "09:00AM-11:00AM",
                "11:00AM-01:00PM",
                "01:00PM-03:00PM",
                "03:00PM-05:00PM"
            ],
            "state_name": "Andaman and Nicobar Islands",
            "to": "17:00:00",
            "vaccine": "COVISHIELD"
        }
    ]
}

and my code is

Importing

import json
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

Other funcs so that you don't go out of context

def screen_clear():
   # for mac and linux(here, os.name is 'posix')
   if os.name == 'posix':
       cl = os.system('clear')
   else:
       cl = os.system('cls')
       # for windows platfrom

def main_menu():
    screen_clear()
    print("Vaccine Finder\n")
    op = int(input("\n1 View State IDs'\n\n2 View District IDs'\n\n3 Check Vaccine In Your District\n\n4 Quit: \n\n"))
    while True:
        if op == 1:
            steller()
        elif op == 2:
            dteller()
        elif op == 3:
            vteller()
        elif op == 4:
            break
        else:
            print("Invalid Input!")

================= Until Now was everything that is needed to know this prob ==================

The problematic code:

def vteller():
    global driver
    district_id = int(input('Enter District ID: '))
    date = input('Enter Date format(DD-MM-YYYY): ')
    vweb = f"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id={district_id}&date={date}"
    driver.get(vweb) # The Website
    data = driver.find_element_by_tag_name('pre').text
    parsed = json.loads(data)
    j1 = json.dumps(parsed, indent=4, sort_keys=True)
    fl = open('vfinder.json', 'w+')
    fl.write(j1)
    fl.close()
    print("\n")
    print("\n")
    op = int(input('Enter 0(Zero) To Go To Main Menu: '))
    if op == 0:
        main_menu()
    else:
        print("Invalid Input!")
        main_menu() 

I want to take out specific keys and their values and print\store them eg. I want "address" and "block_name" and "name" and "fee" and many more so how do I do it?

Aadesh Gurav
  • 11
  • 1
  • 8

1 Answers1

0

Save the json output as json file

then

with open("your_file_name.json", "r") as f:#Put Respective File Name
    keys = json.load(f)

This will read the file and we made key as a object then you can use that to obtain data , treating json as dict.

Eg of Json Data

{
    "sessions": [
        {
            "address": "Campbellbay",
            "available_capacity": 50,
            "available_capacity_dose1": 25,
            "available_capacity_dose2": 25,
            "block_name": "Campbell Bay",
            "center_id": 552109,
            "date": "19-05-2021",
            "district_name": "Nicobar",
            "fee": "0",
            "fee_type": "Free",
            "from": "09:00:00",
            "lat": 7,
            "long": 93,
            "min_age_limit": 45,
            "name": "Campbellbay PHC",
            "pincode": 744302,
            "session_id": "bfea957c-a34e-4b5f-99a0-4c739102eec5",
            "slots": [
                "09:00AM-11:00AM",
                "11:00AM-01:00PM",
                "01:00PM-03:00PM",
                "03:00PM-05:00PM"
            ],
            "state_name": "Andaman and Nicobar Islands",
            "to": "17:00:00",
            "vaccine": "COVISHIELD"
        },
        {
            "address": "Car Nicobar",
            "available_capacity": 45,
            "available_capacity_dose1": 20,
            "available_capacity_dose2": 25,
            "block_name": "Car Nicobar",
            "center_id": 570779,
            "date": "19-05-2021",
            "district_name": "Nicobar",
            "fee": "0",
            "fee_type": "Free",
            "from": "09:00:00",
            "lat": 9,
            "long": 92,
            "min_age_limit": 45,
            "name": "BJR Hospital",
            "pincode": 744301,
            "session_id": "f7d34d28-d936-4a25-95eb-7a1cdd09c545",
            "slots": [
                "09:00AM-11:00AM",
                "11:00AM-01:00PM",
                "01:00PM-03:00PM",
                "03:00PM-05:00PM"
            ],
            "state_name": "Andaman and Nicobar Islands",
            "to": "17:00:00",
            "vaccine": "COVISHIELD"
        },
        {
            "address": "Nancowry",
            "available_capacity": 50,
            "available_capacity_dose1": 25,
            "available_capacity_dose2": 25,
            "block_name": "Nancowry",
            "center_id": 552108,
            "date": "19-05-2021",
            "district_name": "Nicobar",
            "fee": "0",
            "fee_type": "Free",
            "from": "09:00:00",
            "lat": 7,
            "long": 93,
            "min_age_limit": 45,
            "name": "Nancowry CHC",
            "pincode": 744303,
            "session_id": "2a73f5c0-d0f8-4272-9cf1-2e68f20d8751",
            "slots": [
                "09:00AM-11:00AM",
                "11:00AM-01:00PM",
                "01:00PM-03:00PM",
                "03:00PM-05:00PM"
            ],
            "state_name": "Andaman and Nicobar Islands",
            "to": "17:00:00",
            "vaccine": "COVISHIELD"
        }
    ]
}

So Here If You Want To Get Address

in python

address = keys['sessions']['address']
district = keys['sessions']['district_name']
print(address)
print(district )

Final Code

with open("your_file_name.json", "r") as f:#Put Respective File Name
    keys = json.load(f)

address = keys['sessions'][0]['address']
district = keys['sessions'][0]['district_name']
print(address)
print(district )

output

Campbellbay Nicobar