简体   繁体   中英

Extracting float from JSON file in Python

Background -
I have a function that returns an API response. If api_response contains meta it will print the response, otherwise the function loops through some code that extracts and prints id and percent_complete key pair values. The purpose being is that the API response will return these values to show how close it is to having the data available for the user to call separately.

Issue - whilst the id is returned and printed without issue, the percent_complete returns blank.

Function -

def unpack_response():
    api_response = api_call()
# Code Block # 1
    while "meta" not in api_response:
        id_value = "id"
        res = [val[id_value] for key, val in api_response.items() if id_value in val]
        id_value = "".join(res)
        percent_value = "percent_complete"
        res = [val[percent_value] for key, val in api_response.items() if percent_value in val]
        percent_value = "".join(res)
        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)

Example print output - an example showing what is currently printed every loop iteration:

Your data requested, associated with ID: 2205686 is  complete!

API response - example of response where I successfully extract the id key pair value, however where the percent_complete key pair value is blank:

{'data': {'id': '2205686',
  'type': 'jobs',
  'attributes': {'job_type': 'PORTFOLIO_VIEW_RESULTS',
   'started_at': '2021-12-16T18:59:50Z',
   '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/2205679/relationships/creator',
     'related': '/v1/jobs/2205679/creator'},
    'data': {'type': 'users', 'id': '731221'}}},
  'links': {'self': '/v1/jobs/2205679'}},
 'included': []}

My thoughts - unlike id (which has a key pair value in quotation marks) the percent_complete does not and is a float. Could my code require some changes to accommodate?

I managed to achieve the descried result as follows -

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"
   #         res2 = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']]
           res2 = [val['attributes'].get(percent_value, '') for key, val in api_response.items()]
           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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM