pythonjsondjangocrondjango-cron

python function when added to cron using django-crontab gives "No JSON object could be decoded" ERROR


When i execute my python function on django shell it executes fine. But when the function is executed via crontab it gives "No JSON object could be decoded" Error.

successfully made the api call. response status is  200
Exception occured : No JSON object could be decoded
Failed to complete cronjob at ('0,30 * * * *', 'myapp.cron.pulldata', ' >> /usr/local/devops/myapp/crontab.log')
Traceback (most recent call last):
 File "/usr/local/devops/provider_monitoring/env/lib/python2.7/site-packages/django_crontab/crontab.py", line 145, in run_job
   func(*job_args, **job_kwargs)
 File "/usr/local/devops/provider_monitoring/provider_monitoring/provider_automation/cron.py", line 49, in pulldata_and_createjira
   response = response.json()
 File "/usr/local/devops/provider_monitoring/env/lib/python2.7/site-packages/requests/models.py", line 866, in json
   return complexjson.loads(self.text, **kwargs)
 File "/opt/python-2.7.10/lib/python2.7/json/__init__.py", line 338, in loads
   return _default_decoder.decode(s)
 File "/opt/python-2.7.10/lib/python2.7/json/decoder.py", line 366, in decode
   obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 File "/opt/python-2.7.10/lib/python2.7/json/decoder.py", line 384, in raw_decode
   raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I am doing a POST call in my code to splunk rest api.

payload = {'search' : 'search index="xyz" | table a , b , c' , 'earliest_time' : '-60m' , 'output_mode' : 'json_rows' }
url = "https://localhost:8089/services/search/jobs/export"
headers = {'content-type': "application/x-www-form-urlencoded",'authorization': "xyz",'cache-control': "no-cache"}
response = requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=5, pool_block=False)
response = requests.request("POST", url, data=payload, headers=headers, verify=False)            
response = response.json()

I get error only when i run the python function in my cron.

i am using python version : 2.7.10 django : 1.8


Solution

  • You get "No JSON object could be decoded" error when you have no response from the api.

    Even i had the same issue.I was making a REST api call to splunk where i wanted to collect summary_index data generated every 30 minutes.So my cron was running every 30 minutes. But at every 30th minute the api was still aggregating the data so it failed every 30th minute. When i hit the api with a time delay (that is the time for data to get aggregated properly) , i was getting the response.

    So now my cron is like this :

     5-59/30 * * * *
    

    This cron will run at 5 minutes after, 35 minutes after