Python按StartDate字段对JSON对象数组排序问题排查
问题排查:JSON数组按StartDate倒序排序失效
需求说明
- 遍历JSON中的所有对象数组,若数组内的对象包含
StartDate字段,则将该数组按StartDate从最新到最早倒序排序。
JSON示例片段
{ "items": [ { "PersonId": "0000000000000000", "PersonNumber": "0000000000", "CorrespondenceLanguage": null, "BloodType": null, "DateOfBirth": "1990-01-01", "DateOfDeath": null, "CountryOfBirth": null, "RegionOfBirth": null, "TownOfBirth": null, "ApplicantNumber": null, "CreatedBy": "CREATOR", "CreationDate": "2023-11-23T11:41:21.743000+00:00", "LastUpdatedBy": "CREATOR", "LastUpdateDate": "2023-12-01T21:36:38.694000+00:00", "workRelationships": { "items": [ { "PeriodOfServiceId": "0", "LegislationCode": "US", "LegalEntityId": "0", "LegalEmployerName": "Employer LLC", "WorkerType": "E", "PrimaryFlag": true, "StartDate": "2013-10-21", "assignments": { "items": [ { "AssignmentId": 300000006167868, "AssignmentNumber": "A0000-0", "AssignmentName": "Project Manager", "ActionCode": "TERMINATION", "ReasonCode": "TEST", "EffectiveStartDate": "2022-12-22" } ] } }, { "PeriodOfServiceId": "0", "LegislationCode": "US", "LegalEntityId": "0", "LegalEmployerName": "Employer LLC", "WorkerType": "E", "PrimaryFlag": true, "StartDate": "2023-12-08", "assignments": { "items": [ { "AssignmentId": 0, "AssignmentNumber": "A000000-0", "AssignmentName": "Project management B1", "ActionCode": "REHIRE", "ReasonCode": null, "EffectiveStartDate": "2023-12-08" } ] } } ] } } ] }
问题代码
import json from datetime import datetime def main(input): adata = json.loads(input['workerData']) output_data = sort_arrays_with_StartDate(adata) return {'items': output_data} def sort_arrays_with_StartDate(data): if isinstance(data, dict): for key, value in data.items(): if key == 'StartDate' and isinstance(value, list): data[key] = sorted(value, key=lambda x: datetime.strptime(x.get('StartDate', ''), '%Y-%m-%d'), reverse=True) elif isinstance(value, (dict, list)): data[key] = sort_arrays_with_StartDate(value) # Update the original data elif isinstance(data, list): for i, item in enumerate(data): data[i] = sort_arrays_with_StartDate(item) # Update the original data return data # Return the updated data
问题分析
原代码逻辑完全错误:
- 错误地判断键为StartDate的数组才需要排序,但实际要处理的是数组内对象包含StartDate字段的数组,比如示例中的
workRelationships.items - 递归遍历数组时,仅对数组内的每个对象做递归处理,没有检查当前数组是否需要排序
修正后的代码
import json from datetime import datetime def main(input): adata = json.loads(input['workerData']) output_data = sort_arrays_with_StartDate(adata) return {'items': output_data} def sort_arrays_with_StartDate(data): if isinstance(data, dict): for key, value in data.items(): data[key] = sort_arrays_with_StartDate(value) elif isinstance(data, list): # 先处理数组内的嵌套结构 for i, item in enumerate(data): data[i] = sort_arrays_with_StartDate(item) # 检查数组内的对象是否包含StartDate字段(假设数组内对象结构一致) if data and isinstance(data[0], dict) and 'StartDate' in data[0]: try: # 按StartDate倒序排序,解析为日期对象确保排序准确 data.sort(key=lambda x: datetime.strptime(x['StartDate'], '%Y-%m-%d'), reverse=True) except ValueError: # 处理日期格式错误的情况,不排序直接返回 pass return data
关键修改点
- 调整排序判断逻辑:当遍历到数组时,先检查数组内的对象是否包含
StartDate字段,符合条件才执行排序 - 先处理嵌套再排序:递归处理数组内的所有嵌套结构后,再对当前数组进行排序,避免嵌套结构未处理完成就排序
- 增加异常处理:防止
StartDate格式错误导致程序崩溃
内容的提问来源于stack exchange,提问作者Jonar Ramos
相关产品推荐
相关产品推荐

