从 JSON 响应中提取浮点键对值时出现 TypeError

TypeError when extracting float key pair value from JSON response

背景 - 我有一个函数,它接受一个名为 api_response 的变量(它本身是使用另一个函数的 api_response = json.loads(response.text) 格式化的,并试图提取 2x它的密钥对值 - 即 idpercent_complete 并打印它们。

函数-

def unpack_response():
    api_response = api_call()
# Code Block # 1
    while "meta" not in api_response:
        id_value = "id"
        res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
        id_value = "".join(res1)
        percent_value = "percent_complete"
        # The following line of code is referenced in the TypeError  
        res2 = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']]
        percent_value = "".join(res2)
        print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')
        time.sleep(5)
        continue
# Code Block # 2
    if "meta" in api_response:
        print(api_response)
unpack_response()

JSON 响应 (api_response) -

{'data': {'id': '2205853', 'type': 'jobs', 'attributes': {'job_type': 'PORTFOLIO_VIEW_RESULTS', 'started_at': '2021-12-17T02:53:48Z', 'parameters': {'end_date': '2021-12-14', 'output_type': 'json', 'view_id': 304078, 'portfolio_id': 1, 'portfolio_type': 'firm', 'start_date': '2021-12-14'}, 'percent_complete': 0.19, 'status': 'In Progress'}, 'relationships': {'creator': {'links': {'self': '/v1/jobs/2205853/relationships/creator', 'related': '/v1/jobs/2205853/creator'}, 'data': {'type': 'users', 'id': '731221'}}}, 'links': {'self': '/v1/jobs/2205853'}}, 'included': []}

问题 - 函数returns id 键值对没有问题并打印(前提是我删除了所有 percent_complete 相关代码),但是 "percent_complete" 密钥对值导致以下 TypeError.

错误 -

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-8a61597dcee6> in <module>
     16     if "meta" in api_response:
     17         print(api_response)
---> 18 unpack_response()

<ipython-input-38-8a61597dcee6> in unpack_response()
      8         percent_value = "percent_complete"
      9 #         res2 = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']]
---> 10         res2 = [val['attributes'].get(percent_value, '') for key, val in api_response.items()]
     11         percent_value = "".join(res2)
     12         print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')

<ipython-input-38-8a61597dcee6> in <listcomp>(.0)
      8         percent_value = "percent_complete"
      9 #         res2 = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']]
---> 10         res2 = [val['attributes'].get(percent_value, '') for key, val in api_response.items()]
     11         percent_value = "".join(res2)
     12         print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')

TypeError: list indices must be integers or slices, not str

任何人都可以帮助我理解为什么 id returns 没有问题(前提是我删除了所有 percent_complete 相关代码),但 [=17= 的密钥对值] 才不是。这与密钥对值是浮点数有关吗?

感谢大家的评论(感谢Barmarrchrome),我重写了我的函数如下-

def unpack_response():
    api_response = api_call()
    if "meta" not in api_response:
        id_value = "id"
        res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
        id_value = "".join(res1)
        percent_value = "percent_complete"
        res2 = api_response["data"]["attributes"].get("percent_complete", '')
        print(f' Your data requested, associated with ID: {id_value} is {res2} complete!')
        time.sleep(5)
        api_response = api_call()
    if not "meta" in api_response:
        print(api_response)
unpack_response()