AlertaDengue / PySUS

Library to download, clean and analyze openly available datasets from Brazilian Universal health system, SUS.
GNU General Public License v3.0
175 stars 68 forks source link

JSON decode Error in get_sidra_table function #178

Open fccoelho opened 10 months ago

fccoelho commented 10 months ago
from pysus.online_data import IBGE
IBGE.get_sidra_table(200,territorial_level=6,classification=2, categories='all')

generates the following exception:

JSONDecodeError                           Traceback (most recent call last)
Cell In[73], line 1
----> 1 rio = IBGE.get_sidra_table(200,territorial_level=6, geocode=3334557,period='last',classification=2, categories='all')
      2 rio

File ~/MEGAsync/Cursos/curso-pysus/.venv/lib/python3.11/site-packages/pysus/online_data/IBGE.py:103, in get_sidra_table(table_id, territorial_level, geocode, period, variables, classification, categories, format, decimals, headers)
    101 try:
    102     with (get_legacy_session() as s, s.get(url) as response):
--> 103         df = pd.DataFrame(response.json())
    104 except HTTPError as exc:
    105     response = requests.get(url)

File ~/MEGAsync/Cursos/curso-pysus/.venv/lib/python3.11/site-packages/requests/models.py:975, in Response.json(self, **kwargs)
    971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

we probably need to use pd.read_json here instead of passing the JSON directly to the DataFrame constructor.

esloch commented 10 months ago

Hi Flávio, I believe the exception is caused by an incorrect geocode. If you pass geocode=3334557, it seems you intended to use geocode=3304557:

url='https://apisidra.ibge.gov.br/values/t/200/n6/3334557/p/last/c2/all'
Unidade territorial 3334557 do nível territorial Município inexistente ou extinta

A good approach for using the API in get_sidra_table would be to insert some validations to check the parameters before forming the URL.

We use pd.DataFrame(response.json()) in the following functions: get_sidra_table, list_agregados, localidades_por_agregado, lista_periodos inside the IBGE.py module.

esloch commented 10 months ago

Flávio, can we close this issue?