Search code examples
pythonpython-3.xkeyerror

Parsing JSON to create a function


I have the following code which i cannot seem to create a def out of:

I have a Json file some of which looks like this:

{'records': {'expiryDates': ['05-Aug-2021', '12-Aug-2021', '18-Aug-2021', '26-Aug-2021', '02-Sep-2021', '09-Sep-2021', '16-Sep-2021', '23-Sep-2021', '30-Sep-2021', '28-Oct-2021', '30-Dec-2021', '31-Mar-2022', '30-Jun-2022', '29-Dec-2022', '29-Jun-2023', '28-Dec-2023', '27-Jun-2024', '26-Dec-2024', '26-Jun-2025'], 'data': [{'strikePrice': 7500, 'expiryDate': '29-Dec-2022', 'PE': {'strikePrice': 7500, 'expiryDate': '29-Dec-2022', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY29-12-2022PE7500.00', 'openInterest': 21, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 235, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 2800, 'totalSellQuantity': 0, 'bidQty': 2800, 'bidprice': 5.85, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8000, 'expiryDate': '30-Dec-2021', 'PE': {'strikePrice': 8000, 'expiryDate': '30-Dec-2021', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY30-12-2021PE8000.00', 'openInterest': 21, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 3.1, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 6850, 'totalSellQuantity': 0, 'bidQty': 1650, 'bidprice': 3.05, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8300, 'expiryDate': '30-Jun-2022', 'PE': {'strikePrice': 8300, 'expiryDate': '30-Jun-2022', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY30-06-2022PE8300.00', 'openInterest': 3, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 4.7, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 2500, 'totalSellQuantity': 0, 'bidQty': 2500, 'bidprice': 0.25, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8500, 'expiryDate': '30-Dec-2021', 'PE': {'strikePrice': 8500, 'expiryDate': '30-Dec-2021', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY30-12-2021PE8500.00', 'openInterest': 199.5, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 3.25, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 7600, 'totalSellQuantity': 0, 'bidQty': 1500, 'bidprice': 3.05, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8500, 'expiryDate': '29-Dec-2022', 'PE': {'strikePrice': 8500, 'expiryDate': '29-Dec-2022', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY29-12-2022PE8500.00', 'openInterest': 2254, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 22.9, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 2850, 'totalSellQuantity': 0, 'bidQty': 2800, 'bidprice': 18.55, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, 

To access the data i have:

access_records_data = data['records']

So this produces something like this:

{'expiryDates': ['05-Aug-2021',
  '12-Aug-2021',
  '18-Aug-2021',
  '26-Aug-2021',
  '02-Sep-2021',
  '09-Sep-2021',
  '16-Sep-2021',
  '23-Sep-2021',
  '30-Sep-2021',
  '28-Oct-2021',
  '30-Dec-2021',
  '31-Mar-2022',
  '30-Jun-2022',
  '29-Dec-2022',
  '29-Jun-2023',
  '28-Dec-2023',
  '27-Jun-2024',
  '26-Dec-2024',
  '26-Jun-2025'],
 'data': [{'strikePrice': 7500,
   'expiryDate': '29-Dec-2022',
   'PE': {'strikePrice': 7500,
    'expiryDate': '29-Dec-2022',
    'underlying': 'NIFTY',
    'identifier': 'OPTIDXNIFTY29-12-2022PE7500.00',
    'openInterest': 21,
    'changeinOpenInterest': 0,
    'pchangeinOpenInterest': 0,
    'totalTradedVolume': 0,
    'impliedVolatility': 0,
    'lastPrice': 235,
    'change': 0,
    'pChange': 0,
    'totalBuyQuantity': 2800,
    'totalSellQuantity': 0,
    'bidQty': 2800,
    'bidprice': 5.85,
    'askQty': 0,
    'askPrice': 0,
    'underlyingValue': 16130.75}},

Great this is working! Next to access the data inside the records i have this:

question_access = access_records_data['data']

which produces this :

question_access

    {'strikePrice': 15500,
  'expiryDate': '31-Mar-2022',
  'CE': {'strikePrice': 15500,
   'expiryDate': '31-Mar-2022',
   'underlying': 'NIFTY',
   'identifier': 'OPTIDXNIFTY31-03-2022CE15500.00',
   'openInterest': 0,
   'changeinOpenInterest': 0,
   'pchangeinOpenInterest': 0,
   'totalTradedVolume': 0,
   'impliedVolatility': 0,
   'lastPrice': 0,
   'change': 0,
   'pChange': 0,
   'totalBuyQuantity': 100,
   'totalSellQuantity': 350,
   'bidQty': 50,
   'bidprice': 1150,
   'askQty': 100,
   'askPrice': 2299.9,
   'underlyingValue': 16130.75},
  'PE': {'strikePrice': 15500,
   'expiryDate': '31-Mar-2022',
   'underlying': 'NIFTY',
   'identifier': 'OPTIDXNIFTY31-03-2022PE15500.00',
   'openInterest': 11,
   'changeinOpenInterest': 0,
   'pchangeinOpenInterest': 0,
   'totalTradedVolume': 0,
   'impliedVolatility': 0,
   'lastPrice': 497.05,
   'change': 0,
   'pChange': 0,
   'totalBuyQuantity': 0,
   'totalSellQuantity': 50,
   'bidQty': 0,
   'bidprice': 0,
   'askQty': 50,
   'askPrice': 470,
   'underlyingValue': 16130.75}},

Note i am not pasting the entire data because it is very long but i just wanted to to understand what results i am getting. Next I use this list to get to all the keys i can use in the dict in the list. To do this i have done this:

for question_data in question_access:
    print(question_data)

Which produces:

{'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'CE': {'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY27-06-2024CE18500.00', 'openInterest': 3, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 1810, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 0, 'totalSellQuantity': 500, 'bidQty': 0, 'bidprice': 0, 'askQty': 500, 'askPrice': 1814.6, 'underlyingValue': 16130.75}}

Next to get to CE data which i am interested in:

   CE_access = question_data['CE']
which contains this data:
{'strikePrice': 18500,
 'expiryDate': '27-Jun-2024',
 'underlying': 'NIFTY',
 'identifier': 'OPTIDXNIFTY27-06-2024CE18500.00',
 'openInterest': 3,
 'changeinOpenInterest': 0,
 'pchangeinOpenInterest': 0,
 'totalTradedVolume': 0,
 'impliedVolatility': 0,
 'lastPrice': 1810,
 'change': 0,
 'pChange': 0,
 'totalBuyQuantity': 0,
 'totalSellQuantity': 500,
 'bidQty': 0,
 'bidprice': 0,
 'askQty': 500,
 'askPrice': 1814.6,
 'underlyingValue': 16130.75}

Finally if i do something like this:

CE_access['askQty']

it produces this 500. This is the result which i was looking for. So what i am trying to do is working. Next my plan was to use this code and produce a function. To do this i had done the following:

The code is :

def ask_price():
    access_records_data = data['records']
    question_access = access_records_data['data']
    for question_data in question_access:
        CE_access = question_data['CE']
         [there is another for loop here for CE_access]
    

i get this error :

<ipython-input-306-153b641331d7> in ask_price()
      3     question_access = access_records_data['data']
      4     for question_data in question_access:
----> 5         CE_access = question_data['CE']
      6 

KeyError: 'CE'

I have been beating my head over this code for a while now and have not been able to make any progress, please help me. Why am i getting this error, the code above before the function is working.

Edit :

print(question_data)


{'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'CE': {'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY27-06-2024CE18500.00', 'openInterest': 3, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 1810, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 0, 'totalSellQuantity': 500, 'bidQty': 0, 'bidprice': 0, 'askQty': 500, 'askPrice': 1814.6, 'underlyingValue': 16130.75}}

Solution

  • The dictionaries in your json file either have "PE" or "CE" keys. No dictionary has both.

    You could use get to access the dictionary values which automatically returns None if the key doesn't exist in the dictionary.

    Try something like this:

    for question_data in data['records']['data']:
        if question_data.get('CE'):
            print(question_data.get('CE')['askQty'])