2
I wonder how I can walk a JSON within another JSON in Python.
In this case, I have a file that is on the following link:
https://sisu-api-pcr.apps.mec.gov.br/api/v1/oferta/instituicao/570
And I’m using the library urllib.request to read my file main py. as follows:
import urllib.request #importando biblioteca urllib.request
import json #importando biblioteca json
url = 'https://sisu-api-pcr.apps.mec.gov.br/api/v1/oferta/instituicao/570' #atribuindo valor link a variável url
resp = urllib.request.urlopen(url+str()).read() #Abrindo e lendo URL - Atribuindo valor a variável resposta
resp = json.loads(resp.decode('utf-8')) #Codificando URL para codificação UTF-8
for x in resp: #Percorrendo toda variável resp
print(x['co_oferta']+' - '+x['no_curso']) #Imprimindo somente os campos necessários
print("") #Imprimindo linha em branco
At terminal output, it returns me an error:
Traceback (most recent call last):
File "...\Documents\Projetos\Python\python-json-ufs\main.py", line 9, in <module>
print(x['co_ies']+' - '+x['no_ies']) #Imprimindo somente os campos necess�rios
TypeError: string indices must be integers
[Done] exited with code=1 in 0.956 seconds
I noticed that the file starts with:
{
"search_rule":"UFRN - UNIVERSIDADE FEDERAL DO RIO GRANDE DO NORTE",
"0":{
"co_oferta":"184760",
"co_termo_adesao":"4316",
.
.
.
.
Like a filing cabinet JSON within another.
I wonder if anyone can help me read this file correctly.
Follow the test link working https://replit.com/join/llrgypq-lucasandradetho
– Lucas Andrade -