I have the following JSON which i am getting using REST API
looks something like this
"itineraryIntent": {
"itineraryId": 2446,
"userNodes": [
{
"user": {
"id": "-2",
"givenName": "Wiliam Never",
"preferredAirport": "",
"travelerGroupId": null,
"externalId": null,
"externalId2": null,
"checkCCFieldForTraveler": false,
"notificationEmailOverride": null,
"paxType": 0,
"title": "Dr",
"middleName": null,
"dateOfBirth": "1980-01-15T00:00:00Z",
"phoneNumber": null,
"tsaInfo": null,
"source": "RequestEndPoint",
"allowBookingGuestTraveler": null,
"gdsTravelerProfileContainsFirstName": false,
"profilesFound": {
"Agency": null,
"Corporate": null,
"Traveler": null
},
"userId": "-2",
"firstName": "Wiliam",
"lastName": "Never"
},
"elements": []
I am using the following to get the information that i need
details = response.json()
checkIntent = details["itineraryIntent"]["userNodes"][0]["elements"]
if len(checkIntent) == 0:
print("No intent found, nothing is classified")
return False
else:
return True
This code works 80% of the time. The problem is that sometimes i get
TypeError: 'NoneType' object is not subscriptable
What i have noticed is that when trying to get the response sometimes details["itineraryIntent"]["userNodes"] itself is null. I have tried to use isIntances and other methods but none of them work for me.
Currently i have implemented sleep for 10 seconds before getting the response, but that is not a good solution, cause most of the cases the proper response comes within 4-5 seconds.
How do i make this robust enough so that I can get the desired results without sleep.
details = response.json()
if not details["itineraryIntent"]:
time.sleep(7)
response2 = requests.request("GET", url, headers=headers, data=payload)
details2 = response2.json()
checkIntent = details2["itineraryIntent"]["userNodes"][0]["elements"]
else:
checkIntent = details["itineraryIntent"]["userNodes"][0]["elements"]
if len(checkIntent) == 0:
print("No intent found, nothing is classified")
return False
else:
return True