TypeError: the JSON object must be str, not 'bytes'

1

People have a problem with this code:

def task_get_data_misindicadores():
    url = 'https://*****.co/api/*****/{0}/'.format(
        config.MI_APIKEY
    )
    req = requests.get(url)
    if not req.ok:
        return 'Timeout Error'

    to_cache = []
    try:
        for indicator in req.json():
            if indicator['updated_at'] is None:
                continue

            if indicator['is_public']:
                updated_at = timesince(datetime.strptime(
                    indicator['updated_at'], "%Y-%m-%dT%H:%M:%S"
                ))

                to_cache.append({
                    'name': indicator['name'],
                    'value': "{0:0.0f}".format(float(indicator['measured_value'])),
                    'url': 'https://*****.co{0}'.format(
                    indicator['url']
                ),
                    'trend': indicator['trend'],
                    'heat': indicator['heat'],
                    'metric_units': indicator['metric_units'],
                    'goal': indicator['goal'],
                    'updated_at': updated_at,
                })
    except TypeError:
        pass

    if len(to_cache) > 0:
        cache.set('mis_indicadores_list', to_cache, 3600)
        return True

    return False

It turns out that whenever you get to the line for indicator in req.json(): throws the exception, I tried anyway to convert it to string but fail.

I use this inside a celery task, when I do it because shell_plus runs perfect but in celery it always fails.

I use:

  • Django 1.8.13
  • Python 3.5
  • Python 3.5

    Any ideas on how to avoid this error?

        
    asked by Felipe Zuluaga 27.05.2016 в 17:08
    source

    1 answer

    0

    Apparently there was a problem with the files when Python went from 2.7 to 3.5, erase everything and copy again and it was all, now works fine.

        
    answered by 17.06.2016 / 14:31
    source