Problem parsing Alexa json response in python to get value name Problem parsing Alexa json response in python to get value name json json

Problem parsing Alexa json response in python to get value name


As you already pointed out, your problem was that you treated the resolution object as it was subscriptable, accessing it via

resolutions.resolutions_per_authority[0].values[0].value

is the right way to obtain it.

It may be helpful to point out that, in case of multiple matches, Alexa will return the resolutions in the order of the most likely to match the user intent.

This code snippet iterates the slots and return a python dictionary with just the keys to know if it was validated and the id of the matched value:

from ask_sdk_model.slu.entityresolution import StatusCode@staticmethoddef get_slot_values(filled_slots):    """Return slot values with additional info."""    slot_values = {}    logger.info("Filled slots: {}".format(filled_slots).replace("\n", "\r"))    for key, slot_item in six.iteritems(filled_slots):        name = slot_item.name        try:            status_code = slot_item.resolutions.resolutions_per_authority[0].status.code            if status_code == StatusCode.ER_SUCCESS_MATCH:                slot_values[name] = {                    "synonym": slot_item.value,                    "resolved": slot_item.resolutions.resolutions_per_authority[0].values[0].value.__dict__,  # to make it JSON serializable                    "is_validated": True,                }            elif status_code == StatusCode.ER_SUCCESS_NO_MATCH:                slot_values[name] = {                    "synonym": slot_item.value,                    "resolved": slot_item.value,                    "is_validated": False,                }            else:                pass        except (AttributeError, ValueError, KeyError, IndexError, TypeError) as e:            # for BUILT-IN intents, there are no resolutions, but the value is specified            if slot_item.value is not None and slot_item.value != 'NONE':                slot_values[name] = {                    "synonym": slot_item.value,                    "resolved": slot_item.value,                    "is_validated": True,                }            else:                logger.info("SLOT {} UNRESOLVED".format(name))                slot_values[name] = {                    "synonym": slot_item.value,                    "resolved": slot_item.value,                    "is_validated": False,                }    return slot_values

where filled_slots = handler_input.request_envelope.request.intent.slots


OK, I finally located a python example that uses the object-oriented version of the relatively new SDK, as I am. The example is the python version of Amazon's PetMatch example.

Based on that, the following is working:

slots = handler_input.request_envelope.request.intent.slotsdirection = slots["direction"].resolutions.resolutions_per_authority[0].values[0].value.name

I'm still looking to understand better HOW it works, but at least it's working, and may also help someone else. I find that with Alexa examples and documentation, there is a ton out there, but it's not well organized and the api keeps changing, so some of what you find turns out to be obsolete.