Malformed API Response?

dmccarville
Mega Contributor

I'm using Python to query the REST API. Often times it works, but sometimes it returns malformed JSON. Have other people ran into this? Is there a solution?

 

headers = {
        'Authorization': f'Bearer {bearer_token}',   
        'Content-Type': 'application/json',
        }

    params = {
        'sysparm_limit': increment,
        'sysparm_offset': offset,
        'sysparm_display_value': 'true',
        'sysparm_exclude_reference_link': 'true',
        'sysparm_query': f'sys_created_onBETWEENjavascript:gs.dateGenerate("{start_date}","00:00:00")@javascript:gs.dateGenerate("{end_date}","23:59:59")'
        }

    data_response = requests.get(target_url, headers=headers, params=params)
    data = data_response.json().get('result')

 

.json() in the last line fails when the JSON is malformed. For example:

  • The typical response ends in: "category": "Inquiry / Help"}]}'
  • The atypical response would end in "category": "Inquiry / Help"}],""}'

Notice the atypical response includes an extra empty element "". I haven't figured out what causes this, or how to handle it. It seems to happen sporadically. 

0 REPLIES 0