How to browse a JSON within another JSON in Python?

Asked

Viewed 128 times

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.

  • 1

    Follow the test link working https://replit.com/join/llrgypq-lucasandradetho

3 answers

2


There is no unique way to iterate through a JSON data structure, but this structure is a dictionary composed of nested dictionaries so after analyzing JSON with json.loads() for the items using dict.items() unpacking in k and v, where k is the key in the main dictionary and v is the value or nested dictionary.

Like commented by the author of the question the keys of interest are co_oferta and no_curso.

import json
import urllib.request

#Carrega um JSON
url = 'https://sisu-api-pcr.apps.mec.gov.br/api/v1/oferta/instituicao/570'
with urllib.request.urlopen(url) as response:
  resp = response.read()

#Faz a análise desse JSON.
resp = json.loads(resp.decode('utf-8'))

#Itera por todos os itens do dicionário principal....
for k, v in resp.items():
  #...se a 'co_oferta' está contida no dicionário aninhado...
  if 'co_oferta' in v:
    print(v['co_oferta'], end=" ")         #...se sim, imprime seu valor.
  else:
    print("XXX")                        #...se não, imprime `XXX`
  #...se a 'no_curso' está contida no dicionário aninhado...
  if 'no_curso' in v:
    print(v['no_curso'])                  #...se sim, imprime seu valor.
  else:
    print("XXXXXXXXXXXXXXX XXXXXXXXX ") #...se não, imprime `XXX.......`
  print()

Test the code on Repli.it

  • 1

    The following keys are used for this code above: co_oferta and no_curso

1

As above follows the whole code:

import urllib.request
import json

url = 'https://sisu-api-pcr.apps.mec.gov.br/api/v1/oferta/instituicoes'
resp = urllib.request.urlopen(url).read()
resp = json.loads(resp.decode('utf-8'))



for i, x in enumerate(resp):
    print(x['co_ies']+" -  "+x['no_ies'])

exit:

21503 - PEDRO II COLLEGE 593 - FEDERAL CENTER OF TECHNOLOGICAL EDUCATION CELSO SUCKOW DA FONSECA 26 - NATIONAL SCHOOL OF STATISTICAL SCIENCES 3223 - HIGHER SCHOOL OF HEALTH SCIENCES 12 - FEDERAL UNIVERSITY OF RIO GRANDE 1120 - INSTITUTO FEDERAL DE EDUCAÇÃO, CIÊNCIA E TECNOLOGIA FLUMINENSE 1303 - FEDERAL INSTITUTE OF EDUCATION, SCIENCE AND TECHNOLOGY GOIANO...

with the url 'https://sisu-api-pcr.apps.mec.gov.br/api/v1/oferta/instituicao/570'

import urllib.request
import json

url = 'https://sisu-api-pcr.apps.mec.gov.br/api/v1/oferta/instituicao/570'
resp = urllib.request.urlopen(url).read()
resp = json.loads(resp.decode('utf-8'))

print(json.dumps(resp['search_rule']))

for i in range(len(resp['search_rule']) - 48):
    print("=============")
    print((resp[str(i)]['no_curso']))

this above see that keys are strings

  • 1

    I tested it here and it worked smoothly.

  • I put the range to Len(Resp['search_rule']) - 48 just not to list everything and fill my terminal , because if give a len(resp['search_rule']) will see that it has size 50 .

-1

Good morning, the error is in trying to traverse the list contents by placing text in the field: print(x['co_ies']+. Resp is a list of json. The solution would be to use enumerate:

for i, x in enumerate(resp):
    print(x['co_ies']+ " - " + x['no_ies'])
  • I tried your solution friend, and returned the following error: Traceback (Most recent call last): File "c: Users... Documents Python projects python-json-ufs main.py", line 9, in <module> print(x['co_ies']+ " - " + x['no_ies']) Typeerror: string indices must be integers [Done] exited with code=1 in 0.846 Seconds

  • see that only right if you do before Resp = json.loads(Resp.Decode("utf-8") or else you will get this error, here I did and it worked

  • **see how it turned out : ** >>> for i,x in enumerate(resp):&#xA;... print(x['co_ies'] + ' - ' + x['no_ies'])&#xA;... &#xA;21503 - COLÉGIO PEDRO II&#xA;593 - CENTRO FEDERAL DE EDUCAÇÃO TECNOLÓGICA CELSO SUCKOW DA FONSECA&#xA;26 - ESCOLA NACIONAL DE CIÊNCIAS ESTATÍSTICAS&#xA;3223 - ESCOLA SUPERIOR DE CIÊNCIAS DA SAÚDE&#xA;12 - UNIVERSIDADE FEDERAL DO RIO GRANDE

  • 1

    OK, I’ll post, the full code, sorry I sent only the snippet ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.