Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON decode Error in get_sidra_table function #178

Open
fccoelho opened this issue Dec 5, 2023 · 2 comments
Open

JSON decode Error in get_sidra_table function #178

fccoelho opened this issue Dec 5, 2023 · 2 comments
Assignees
Labels

Comments

@fccoelho
Copy link
Collaborator

fccoelho commented Dec 5, 2023

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
Copy link
Collaborator

esloch commented Dec 6, 2023

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
Copy link
Collaborator

esloch commented Dec 21, 2023

Flávio, can we close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants