I'm creating an app for searching flight connections with Amazon Alexa. I'm trying to load a json file containing airport informations into a variable to search data from it at a later point. I tried to load it into a global variable but that didn't work. Then I tried to load it into the session_attributes, but this also fails. The json file contains 12668 entries. What am I doing wrong? What would be a good way to do this? Is it taking too much time to load the json into a variable and Alexa quits because of this?
The json file is located in the lambda folder in the alexa developer console, where also the file with the code is.
Alexa responds with "There was a problem with the requests skill's response"
"error": {
"type": "INVALID_RESPONSE",
"message": "An exception occurred while dispatching the request to the skill."
}
here is my basic code:
airportdata = []
def init():
airportdata = load_airport_data()
def load_airport_data():
with open('airportdata.json', encoding="utf8") as json_file:
return json.load(json_file)
class LaunchRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return ask_utils.is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
# init()
session_attr = handler_input.attributes_manager.session_attributes
session_attr['airportdata'] = load_airport_data()
speak_output = "Welcome to the App. From where will you departure?"
return (
handler_input.response_builder
.speak(speak_output)
.ask(speak_output)
.response
)
EDIT: It turned out the path used was not correct. It should be "/var/task/airportdata.json" instead of just the name of the json file. The files of the lambda gets copied there. Then it works as intended.