2
I have a Python code that I can display on the screen all JSON records, only I need to bring only the field 'Nome'
and the count
of each.
First he brings with []
(list format) and I need the information clean, but I don’t know how to get it.
Ex.:
{
'contract_id':300166,
'plan_id':15,
'total_value':39.9,
'installment_value':0.0,
'agent_code':'nenhum',
'customer_name':'Rafael Moura dos Santos',
'customer_cpf':'54745361602',
'status':'cancelado',
'payment_link':None,
'installments':12,
'payment_method':None,
'validity_start_date':'2017-11-07 00:00:00',
'validity_end_date':'2018-11-07 00:00:00',
'addresses':[
{
'street':'rua da ovelha',
'number':'999',
'additional_details':'bloco do completo',
'zipcode':'07121010',
'district':'water',
'city':'agua',
'state':'SP',
'country':'BR'
}
],
'phones':[
{
'category':'mobile',
'extension':'',
'number':'5511981013752'
}
],
'customer':{
'name':'Rafael Moura dos Santos',
'cpf':'54745361602',
'birth':'1988-06-28',
'gender':'',
'marital_status':''
},
'links':[
],
'policies':[
{
'policy_id':210,
'policy_number':'PA-000090',
'certificate_link':'',
'value':39.9,
'status':'ativo',
'product_id':1,
'metadata':{
},
'covered_goods':[
{
'Nome':'Rex',
'Cão ou gato':'Cão',
'Cor do pelo':'Marrom',
'Data de nascimento':'29/03/2010',
'Tamanho':'M',
'Macho ou Fêmea':'Macho',
'Raça':'Arc',
'Doença ou lesão preexistente':'Não',
'Descreva preexistência':'',
'Vacinação em dia':'Sim'
},
{
'Nome':'Rex',
'Cão ou gato':'Cão',
'Cor do pelo':'Marrom',
'Data de nascimento':'29/03/2010',
'Tamanho':'M',
'Macho ou Fêmea':'Macho',
'Raça':'Arc',
'Doença ou lesão preexistente':'Não',
'Descreva preexistência':'',
'Vacinação em dia':'Sim'
}
]
}
]
}
Code:
import json
import requests
import csv
import collections
url =
headers = {'Content-Type' : 'application/json',
}
response = requests.get(url, headers=headers)
res = json.loads(response.text)
output = []
for c in res['response']:
goods = []
if len(c['policies']) > 0:
goods = [good["Nome"] for good in c['policies'] [0]['covered_goods']]
output.append(goods)
d = {x:goods.count(x) for x in goods}
print(d)
print(output)
Hey... thanks for answering, I tried but he takes the value of the last item, in case it was a dog named 'dog', like, he ignores the others...
– Gleice
I know what happens. I’ll help you.
– Rafael Barros
I made the modifications based on what you said. If you have anything else, I suggest you even change the question in case someone else tries to help.
– Rafael Barros
Hi, gave error: Typeerror: string indices must be integers.. already searched but could not fix...
– Gleice
I had removed the variable index
res['response']
to simulate here on my computer. I returned it to the code and you can try again. From now on I cannot help you with the information you have given me. Why? Because I would need to know what the structure of your variable is likeres
. The part you sent relates only to the structure of a contract.– Rafael Barros
One thing that caught my attention is the fact that you put
c['policies'][0]
. Can I only have one policy in each contract? If you can have more, you have to iterate there as well.– Rafael Barros
My Deusss, it worked.... I think I understand your scores, I’ll study them all! Gratitude!
– Gleice
Glad it worked! Tell you that was the first time I put my hand on a JSON! Oh, and don’t forget to mark the answer as chosen! ;)
– Rafael Barros
How do I do it ? Hahahaha
– Gleice
Next to the answers given, near where there is an arrow up and an arrow down, there is a V in the color gray. When you mark it, it turns green. Here shows how and here explains why to do this.
– Rafael Barros