如何使用列表理解 Python 过滤没有 JSON 键的记录



我有一个Json字典,其中包含呼叫信息和谁处理了呼叫,它包含呼叫统计在键"stat"

此字典存储在变量"interactions"但是在某些情况下,对于给定会话下的给定调用,将没有称为"stat"的键。我试图通过删除处理程序和会话数组来更新这个json字典,如果没有键"stat"然后将整个json字典赋值给一个新变量。我已经使用for循环更新了json,但是执行这个操作需要很长时间。我如何在python

中使用列表推导实现这一点?例如,如果您看到下面的字典,sessionid=abc94没有键"stat">

样本输入:

=交互

[
{
"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"
}
]
}
]
}
]
预期输出:

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
}
],

}
]
}
]
}
]

尝试:

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)

打印:

[
{
"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},
],
}
],
}
],
},
]

最新更新