Search code examples
python-3.xlistlist-comprehension

how to filter records which doesn't have a json key using list Comprehensions python


I have a Json dictionary where it contains the call information and who handled the call and it contains the call stats under the key "stat".

This dictionary is stored in a variable "interactions" But in some cases for a given call under a given session there will be no key called "stat". I am trying to update this json dictionary by removing the handler and the session array if there is no key "stat" and then assign the whole json dict to a new variable. I have updated the json using the for loop, but its taking long time to perform this operation. How can i achieve this using list comprehension in python

For example, if you see the below dict, sessionid=abc94 doesn't have a key "stat"

sample input:

interactions =

[
  {
    "callId": "17f2e9a7-2c82-4908-80f4-019ec2b8be57",
    "Start": "2023-04-05T16:38:56",
    "handlers": [
      {
        "handlerId": "dafedee8-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "c7372835-47f3-461c-b506-e604d0633994",
            "stats": [
              {
                "name": "talkcnt",
                "value": 1
              },
              {
                "name": "talktime",
                "value": 903481
              }
            ],
            
          }
        ]
      },
      {
        "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "cdsfd-47f3-461c-b506-e604d0633994",
            "stats": [
              {
                "name": "talkcnt",
                "value": 100
              },
              {
                "name": "holdtime",
                "value": 232323
              }
            ],
            
          }
        ]
      }
    ]
  },
  {
    "callId": "23sdf-2c82-4908-80f4-019ec2b8be57",
    "Start": "2023-04-05T16:38:56",
    "handlers": [
      {
        "handlerId": "sdfsdfee8-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "c7372835-47f3-461c-b506-e6sdfd",
            "stats": [
              {
                "name": "talkcnt",
                "value": 1
              },
              {
                "name": "talktime",
                "value": 903481
              }
            ],
            
          }
        ]
      },
      {
        "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "abc94"
          }
        ]
      }
    ]
  }
]

expected output:

interactions_updated =

[
  {
    "callId": "17f2e9a7-2c82-4908-80f4-019ec2b8be57",
    "Start": "2023-04-05T16:38:56",
    "handlers": [
      {
        "handlerId": "dafedee8-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "c7372835-47f3-461c-b506-e604d0633994",
            "stats": [
              {
                "name": "talkcnt",
                "value": 1
              },
              {
                "name": "talktime",
                "value": 903481
              }
            ],
            
          }
        ]
      },
      {
        "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "cdsfd-47f3-461c-b506-e604d0633994",
            "stats": [
              {
                "name": "talkcnt",
                "value": 100
              },
              {
                "name": "holdtime",
                "value": 232323
              }
            ],
            
          }
        ]
      }
    ]
  },
  {
    "callId": "23sdf-2c82-4908-80f4-019ec2b8be57",
    "Start": "2023-04-05T16:38:56",
    "handlers": [
      {
        "handlerId": "sdfsdfee8-2869-414f-970e-657f92016e6b",
        "Name": "test",
        "sessions": [
          {
            "ani": "tel:+120134323",
            "direction": "inbound",
            "dnis": "tel:+3243423",
            "requestedRoutings": [
              "Standard"
            ],
            "sessionId": "c7372835-47f3-461c-b506-e6sdfd",
            "stats": [
              {
                "name": "talkcnt",
                "value": 1
              },
              {
                "name": "talktime",
                "value": 903481
              }
            ],
            
          }
        ]
      }
    ]
  }
]

Solution

  • Try:

    interactions = [
        {
            "callId": "17f2e9a7-2c82-4908-80f4-019ec2b8be57",
            "Start": "2023-04-05T16:38:56",
            "handlers": [
                {
                    "handlerId": "dafedee8-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "c7372835-47f3-461c-b506-e604d0633994",
                            "stats": [
                                {"name": "talkcnt", "value": 1},
                                {"name": "talktime", "value": 903481},
                            ],
                        }
                    ],
                },
                {
                    "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "cdsfd-47f3-461c-b506-e604d0633994",
                            "stats": [
                                {"name": "talkcnt", "value": 100},
                                {"name": "holdtime", "value": 232323},
                            ],
                        }
                    ],
                },
            ],
        },
        {
            "callId": "23sdf-2c82-4908-80f4-019ec2b8be57",
            "Start": "2023-04-05T16:38:56",
            "handlers": [
                {
                    "handlerId": "sdfsdfee8-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "c7372835-47f3-461c-b506-e6sdfd",
                            "stats": [
                                {"name": "talkcnt", "value": 1},
                                {"name": "talktime", "value": 903481},
                            ],
                        }
                    ],
                },
                {
                    "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "abc94",
                        }
                    ],
                },
            ],
        },
    ]
    
    
    def filter_handlers(handlers):
        return [
            {**h, "sessions": sessions}
            for h in handlers
            if (sessions := [s for s in h["sessions"] if "stats" in s])
        ]
    
    interactions_updated = [
        {**i, "handlers": h} for i in interactions if (h := filter_handlers(i["handlers"]))
    ]
    
    print(interactions_updated)
    

    Prints:

    [
        {
            "callId": "17f2e9a7-2c82-4908-80f4-019ec2b8be57",
            "Start": "2023-04-05T16:38:56",
            "handlers": [
                {
                    "handlerId": "dafedee8-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "c7372835-47f3-461c-b506-e604d0633994",
                            "stats": [
                                {"name": "talkcnt", "value": 1},
                                {"name": "talktime", "value": 903481},
                            ],
                        }
                    ],
                },
                {
                    "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "cdsfd-47f3-461c-b506-e604d0633994",
                            "stats": [
                                {"name": "talkcnt", "value": 100},
                                {"name": "holdtime", "value": 232323},
                            ],
                        }
                    ],
                },
            ],
        },
        {
            "callId": "23sdf-2c82-4908-80f4-019ec2b8be57",
            "Start": "2023-04-05T16:38:56",
            "handlers": [
                {
                    "handlerId": "sdfsdfee8-2869-414f-970e-657f92016e6b",
                    "Name": "test",
                    "sessions": [
                        {
                            "ani": "tel:+120134323",
                            "direction": "inbound",
                            "dnis": "tel:+3243423",
                            "requestedRoutings": ["Standard"],
                            "sessionId": "c7372835-47f3-461c-b506-e6sdfd",
                            "stats": [
                                {"name": "talkcnt", "value": 1},
                                {"name": "talktime", "value": 903481},
                            ],
                        }
                    ],
                }
            ],
        },
    ]