0

I'm loading JSON payload from a stream using json.loads(). I can easily access objects using the following syntax myVar = AgentEvent["EventType"] and I get expected values. However, I'm having problems accessing object named "Contacts" and its properties. For example: How can I access myVar = AgentEvent["CurrentAgentSnapshot"]["Contacts"]["Status"]? When I tried I get an error. Sample JSON below. Any help from Python rock-stars would be much appreciated.

{
    "AWSAccountId": "12345678",
    "AgentARN": "arn:aws:connect:",
    "CurrentAgentSnapshot": {
        "AgentStatus": {
            "ARN": "arn:aws:connect:",
            "Name": "Available",
            "StartTimestamp": "2022-03-05T20:35:30.836Z",
            "Type": "ROUTABLE"
        },
        "Configuration": {
            "AgentHierarchyGroups": null,
            "FirstName": "test",
            "LastName": "test",
            "RoutingProfile": {
                "ARN": "arn:aws:connect:",
                "Concurrency": [
                    {
                        "AvailableSlots": 0,
                        "Channel": "CHAT",
                        "MaximumSlots": 2
                    },
                    {
                        "AvailableSlots": 0,
                        "Channel": "TASK",
                        "MaximumSlots": 1
                    },
                    {
                        "AvailableSlots": 0,
                        "Channel": "VOICE",
                        "MaximumSlots": 1
                    }
                ],
                "DefaultOutboundQueue": {
                    "ARN": "arn:aws:connect:",
                    "Channels": [
                        "VOICE"
                    ],
                    "Name": "BasicQueue"
                },
                "InboundQueues": [
                    {
                        "ARN": "arn:aws:connect:",
                        "Channels": [
                            "CHAT",
                            "TASK",
                            "VOICE"
                        ],
                        "Name": "BasicQueue"
                    },
                    {
                        "ARN": "arn:aws:connect:",
                        "Channels": [
                            "CHAT",
                            "TASK",
                            "VOICE"
                        ],
                        "Name": null
                    }
                ],
                "Name": "Basic Routing Profile"
            },
            "Username": "test"
        },
        "Contacts": [
            {
                "Channel": "VOICE",
                "ConnectedToAgentTimestamp": "2022-03-05T20:42:14.109Z",
                "ContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
                "InitialContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
                "InitiationMethod": "INBOUND",
                "Queue": {
                    "ARN": "arn:aws:connect:",
                    "Name": "BasicQueue"
                },
                "QueueTimestamp": "2022-03-05T20:42:08.078Z",
                "State": "CONNECTED",
                "StateStartTimestamp": "2022-03-05T20:51:11.819Z"
            }
        ],
        "NextAgentStatus": null
    },
    "EventId": "ca232cf3-3510-415b-8ff1-8ca89b59194f",
    "EventTimestamp": "2022-03-05T21:11:56.315Z",
    "EventType": "HEART_BEAT",
    "InstanceARN": "arn:aws:connect:",
    "PreviousAgentSnapshot": {
        "AgentStatus": {
            "ARN": "arn:aws:connect:",
            "Name": "Available",
            "StartTimestamp": "2022-03-05T20:35:30.836Z",
            "Type": "ROUTABLE"
        },
        "Configuration": {
            "AgentHierarchyGroups": null,
            "FirstName": "test",
            "LastName": "test",
            "RoutingProfile": {
                "ARN": "arn:aws:connect:",
                "Concurrency": [
                    {
                        "AvailableSlots": 0,
                        "Channel": "CHAT",
                        "MaximumSlots": 2
                    },
                    {
                        "AvailableSlots": 0,
                        "Channel": "TASK",
                        "MaximumSlots": 1
                    },
                    {
                        "AvailableSlots": 0,
                        "Channel": "VOICE",
                        "MaximumSlots": 1
                    }
                ],
                "DefaultOutboundQueue": {
                    "ARN": "arn:aws:connect:",
                    "Channels": [
                        "VOICE"
                    ],
                    "Name": "BasicQueue"
                },
                "InboundQueues": [
                    {
                        "ARN": "arn:aws:connect:",
                        "Channels": [
                            "CHAT",
                            "TASK",
                            "VOICE"
                        ],
                        "Name": "BasicQueue"
                    },
                    {
                        "ARN": "arn:aws:connect",
                        "Channels": [
                            "CHAT",
                            "TASK",
                            "VOICE"
                        ],
                        "Name": null
                    }
                ],
                "Name": "Basic Routing Profile"
            },
            "Username": "test"
        },
        "Contacts": [
            {
                "Channel": "VOICE",
                "ConnectedToAgentTimestamp": "2022-03-05T20:42:14.109Z",
                "ContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
                "InitialContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
                "InitiationMethod": "INBOUND",
                "Queue": {
                    "ARN": "arn:aws:connect:",
                    "Name": "BasicQueue"
                },
                "QueueTimestamp": "2022-03-05T20:42:08.078Z",
                "State": "CONNECTED",
                "StateStartTimestamp": "2022-03-05T20:51:11.819Z"
            }
        ],
        "NextAgentStatus": null
    },
    "Version": "2017-10-01"}

1 Answer 1

1

There are 2 problems with this line

AgentEvent["CurrentAgentSnapshot"]["Contacts"]["Status"]
  1. The Contacts have a State and not a Status
  2. Contacts is a list, not an object. So you need to address its items by index or iterate.
AgentEvent["CurrentAgentSnapshot"]["Contacts"][0]["State"]
Sign up to request clarification or add additional context in comments.

2 Comments

I've changed the code to the following 'Contact = AgentEvent["CurrentAgentSnapshot"]["Contacts"][0]["State"]' and I get the following error [ERROR] IndexError: list index out of range Traceback (most recent call last): File "/var/task/lambda_function.py", line 24, in lambda_handler Contact = AgentEvent["CurrentAgentSnapshot"]["Contacts"][0]["State"] [ERROR] IndexError: list index out of range Traceback (most recent call last):
@Felix, then the contacts list is empty. You are probably not trying to access the same data you are showing above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.