Home > Mobile >  Problem returning data with json.loads, I can't solve it
Problem returning data with json.loads, I can't solve it

Time:12-14

I'm trying to return a specific value from my json with json.loads and I'm not getting it. I want it to return the value of 'linkBoleto':

This is the structure of my json file

{  
   "retorno":{  
      "contasreceber":[  
         {  
            "contareceber":{  
               "id":"13123121",
               "situacao": "aberto", 
               "dataEmissao":"2017-03-09",
               "vencimentoOriginal":"2017-03-09",
               "vencimento":"2017-03-09",
               "competencia":"2017-03-01",
               "nroDocumento":"0123456",
               "valor":15.00,
               "saldo":0.00,
               "historico":"Referente ao pedido de venda nº 423",
               "categoria":"Vendas",
               "idFormaPagamento": "123",
               "portador":"CEF",
               "nroNoBanco":"00100358-56",
               "vendedor":"Vendedor exemplo",
               "pagamento": {
                    "totalPago": 15.00,
                    "totalJuro": 0,
                    "totalDesconto": 0,
                    "totalAcrescimo": 0,
                    "totalTarifa": 0,
                    "data": "2017-03-09"
               },
               "ocorrencia":"Unica",
               "linkBoleto":"https://www.bling.com.br/doc.view.php?132",
               "cliente":{  
                  "nome":"Organisys Software",
                  "cpf_cnpj":"00.000.000/0000-1",
                  "tipoPessoa":"J",
                  "ie_rg":"0000000000",
                  "endereco":"Rua Barão do Rio Branco",
                  "numero":"000",
                  "complemento":"Sala 000",
                  "cidade":"Bento Gonçalves",
                  "bairro":"Centro",
                  "cep":"00.000-000",
                  "uf":"RS",
                  "email":"[email protected]"
               }
            }
         }
      ]
   }
}

I tried this, but I couldn't...

import requests
import json

cnpj_consulta = '00.000.000/0000-1'
api_key = 'my-api-key'
url = 'https://bling.com.br/Api/v2/contasreceber/json/'
f_key ='&apikey='
f_cnpj = '&filters=cnpj'
op = '['
cl = ']'

r = requests.get(f'{url}{f_key}{api_key}{f_cnpj}{op}{cnpj_consulta}{cl}')

y = json.loads (r.content)

print (y ['linkBoleto'])

I can return data from the api normally but when I try to apply json.loads to limit the return I can't...

CodePudding user response:

You can't access "linkBoleto" directly, you need to follow the structure of JSON:

print(y["retorno"]["contasreceber"][0]["contareceber"]["linkBoleto"])
  • Related