如何用Python提取API返回JSON中records数组元素为独立JSON对象
问题描述
执行GET API调用后得到如下返回结果:
{ "limit": 1000, "offset": 0, "records": [{ "associatedItems": [{ "id": "rk:db", "name": "rk:db", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "USER" }], "category": "group management", "created": "2022-10-13T17:44:01.633+0000", "eventSource": "", "id": 42914, "objectItem": { "name": "system-administrators", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "GROUP" }, "summary": "User added to group" }, { "associatedItems": [{ "id": "rk:da", "name": "rk:da", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "USER" }], "category": "group management", "created": "2022-10-13T17:44:01.610+0000", "eventSource": "", "id": 42913, "objectItem": { "name": "site-admins", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "GROUP" } }], "total": 492 }
需要从返回结果的records数组中提取每个元素,生成独立的JSON对象,例如第一个元素对应的JSON对象如下:
{ "associatedItems": [{ "id": "rk:db", "name": "rk:db", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "USER" }], "category": "group management", "created": "2022-10-13T17:44:01.633+0000", "eventSource": "", "id": 42914, "objectItem": { "name": "system-administrators", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "GROUP" }, "summary": "User added to group" }
希望用Python实现这个处理逻辑。
Python实现方案
可以借助Python内置的json模块完成解析和提取操作,具体代码如下:
import json # 假设api_response是从GET请求获取到的原始JSON字符串 api_response = ''' { "limit": 1000, "offset": 0, "records": [{ "associatedItems": [{ "id": "rk:db", "name": "rk:db", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "USER" }], "category": "group management", "created": "2022-10-13T17:44:01.633+0000", "eventSource": "", "id": 42914, "objectItem": { "name": "system-administrators", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "GROUP" }, "summary": "User added to group" }, { "associatedItems": [{ "id": "rk:da", "name": "rk:da", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "USER" }], "category": "group management", "created": "2022-10-13T17:44:01.610+0000", "eventSource": "", "id": 42913, "objectItem": { "name": "site-admins", "parentId": "10000", "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory", "typeName": "GROUP" } }], "total": 492 } ''' # 解析JSON字符串为Python字典 data = json.loads(api_response) # 提取records数组中的每个元素 individual_records = data.get('records', []) # 遍历输出每个独立的JSON对象 for idx, record in enumerate(individual_records, start=1): print(f"JSON Object #{idx}") print(json.dumps(record, indent=4)) print("-" * 50)
代码说明
- 解析JSON:使用
json.loads()将API返回的JSON字符串转换为Python字典,便于后续操作。 - 提取记录:通过字典的
get()方法获取records数组,即使键不存在也会返回空列表,避免报错。 - 输出独立对象:遍历
records数组,用json.dumps()将每个元素重新格式化为带缩进的JSON字符串,提升可读性。
如果API响应是通过网络请求获取的(比如使用requests库),可以直接使用响应的json()结果进行处理,示例如下:
import requests import json response = requests.get("你的API地址") data = response.json() individual_records = data.get('records', []) # 后续处理同上
内容的提问来源于stack exchange,提问作者anengelsen
相关产品推荐
相关产品推荐

