-1

So my JSON data looks like below. My line of code: print(jsonData["orderData"]["txnType"])prints out the word SALE then I get an error saying TypeError: 'NoneType' object is not subscriptable With my understanding why am I getting this error if the value is clearly there and printed?

  {  
   'orderData':{  
      'date':'2017-08-29T12:55:19-07:00',
      'receipt':'A2ZC5N96',
      'promo':{  
         '@nil':'true'
      },
      'pmtType':'PYPL',
      'txnType':'SALE',
      'item':'37',
      'amount':'104.28',
      'site':'PASSIOPROD',
      'affi':'BCPATRON2',
      'country':'US',
      'state':'OH',
      'lastName':{  
         '@nil':'true'
      },
      'firstName':{  
         '@nil':'true'
      },
      'currency':'USD',
      'email':{  
         '@nil':'true'
      },
      'zip':'43206',
      'rebillAmount':'97.00',
      'processedPayments':'1',
      'futurePayments':'998',
      'nextPaymentDate':'2017-09-29T12:55:19-07:00',
      'status':'ACTIVE',
      'accountAmount':'44.09',
      'role':'AFFILIATE',
      'customerDisplayName':{  
         '@nil':'true'
      },
      'title':'aaa',
      'recurring':'true',
      'physical':'false',
      'customerRefundableState':'REFUNDABLE'
   }
}

FULL CODE:

devKeys = ["KEUYS"]
apiKeys = ["API"]
sales = []
refunds = []
totalSales = []
x = 0

while x < len(devKeys):
y = 0
indSale = 0
indRefund = 0
indTotal = 0
totalTransactions = 0

payload = devKeys[x]+":"+apiKeys[x]
headers = {"Accept": "application/json", "Authorization": payload}
r = requests.get('https://api.clickbank.com/rest/1.3/orders/list', headers=headers)
jsonData = json.loads(r.text)
text = r.text
if ":[" not in text: #This line is here because the JSON looks different if theres only one entry
    print(jsonData["orderData"]["txnType"])
    if jsonData["orderData"]["txnType"] == "SALE":
        indSale+=44
        indTotal+=1
    else:
        indRefund+=44
else:
    totalTransactions = len(jsonData["orderData"])
    while y < totalTransactions:

        if jsonData["orderData"][y]["txnType"] == "SALE":
            indSale+=44
            indTotal+=1
        else:
            indRefund+=44
        y+=1

sales.append(indSale)
refunds.append(indRefund)
totalSales.append(indTotal)
x+=1
Sean
  • 51
  • 5

1 Answers1

0

With your data, I can print out the correct value without a problem, which means the data is valid. So please double check and verify that "jsonData" object is "sub-scriptable" and has the "orderData" key as referenced in the markdown. Seems like you might not get the expected JSON response.

Also refer to for sub-scriptable: In Python, what does it mean if an object is subscriptable or not?

Hope it helps.

Leo
  • 731
  • 5
  • 8
  • Just noticed you referenced [y] but there is no list of objects under jsonData["orderData"] in your json, so this doesn't work. – Leo Aug 29 '17 at 22:20