我有一个 Json 字典,其中包含呼叫信息和谁处理了呼叫,并且它包含键“stat”下的呼叫统计信息。
本词典存储在变量“interactions”中 但在某些情况下,对于给定会话下的给定调用,将没有名为“stat”的键。 如果没有键“stat”,我试图通过删除处理程序和会话数组来更新这个 json 字典,然后将整个 json dict 分配给一个新变量。 我已经使用 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
}
],
}
]
}
]
}
]