-2

I am fetching some data from an API and saving the contents of response JSON to a list. However i am getting "'NoneType' object is not subscriptable" error. I understood that i am indexing a none object. How do i resolve it? I am pretty new to Python.

for i in range(0,len(content)):
    try:
        response = requests.post(url, data=json.dumps({
   "GetSignificantDevelopments_Request_1": {
      "FindRequest": {
         "CompanyIdentifiers_typehint": [
            "CompanyIdentifiers"
         ],
         "CompanyIdentifiers": [
            {
               "RIC": {
                  #"Value": content[i]
                  "Value": "8341.T"
               }
            }
         ],
         "StartDate": "2020-08-01T00:00:00",
         "EndDate": "2020-09-21T00:00:00",
         "Significance": "1 2 3",
         "MaxNumberOfItems": 2000
      }
   }
}), headers=headers)
        data=json.loads(response.text.encode('utf8'))
        for item in data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']:
            list_RepNo=[]
            list_DevelopmenId=[]
            list_RepNo.append(item['Xrefs']['RepNo'])
            list_DevelopmenId.append(item['Xrefs']['DevelopmentId'])
    except Exception as Error:
    print(Error)
    raise
    continue

The error which i am getting is below:

'NoneType' object is not subscriptable

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-c73ee1d65472> in <module>
     36 }), headers=headers)
     37         data=json.loads(response.text.encode('utf8'))
---> 38         for item in data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']:
     39             list_RepNo=[]
     40             list_DevelopmenId=[]

TypeError: 'NoneType' object is not subscriptable

How do i resolve this?

Alan003
  • 79
  • 3
  • 9
  • 1
    Okay, it's good that you understood that. But it's not clear who do you want by "resolve" it. The data simply does not have that field, that's why the value is none. – user202729 Sep 21 '20 at 14:48
  • only logical solution, without getting into details in your code, is to wrap the subscript access with check that it is not None, something like: `if data['GetSignificantDevelopments_Response_1'] is not None and data['GetSignificantDevelopments_Response_1']['FindResponse'] is not None:` - the order of the check is critical because you first want to check the first access, to make sure it safe, and secondly the second one. – Yossi Levi Sep 21 '20 at 14:50
  • check [this post](https://stackoverflow.com/questions/216972/what-does-it-mean-if-a-python-object-is-subscriptable-or-not) ! might help you to understand the problem u are facing ! – Amine Oukhrid Sep 21 '20 at 14:51
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). One of the keys in this chain (`data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']`) doesn't exist, so you get `None` when you try to access it. `None` is not a `dict` or `list`-type, so you can't obtain anything at a subscript of `None`. In other words, `None['...']` is invalid. To fix it, use [`defaultdict`](https://docs.python.org/2/library/collections.html) or [`dict.get()`](https://docs.python.org/3/library/stdtypes.html#dict.get) with a default value. – Pranav Hosangadi Sep 21 '20 at 14:56

1 Answers1

0

This happens when the array you are indexing is of None type.

In your case, if you do

In[1]: type(data)

you would get

Out[1]: <class 'NoneType'>

Solution:

You would have to make sure the array(data in your case) being indexed is not of type None.

Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51