'Accessing JSON nested object property in Python

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"}


Solution 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"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Fool