import json import requests impostos = requests.get("http://localhost:5000/api/v1/receita/total?code=1.1.1&years=2014&drilldown=true").content #taxas = requests.get("http://localhost:5000/api/v1/receita/total?code=1.1.2&years=2014&drilldown=true").content impostos_dict = json.loads(impostos) #taxas_dict = json.loads(taxas) impostos_dict def create_treemap(code, name=''): api_request_data = requests.get("http://localhost:5000/api/v1/receita/total?code={}&years=2014&drilldown=true".format(code)).content treemap = {} treemap['name'] = name treemap['children'] = [] request_dict = json.loads(api_request_data) for child in request_dict['data']: if code.count('.') < 3: treemap['children'] += [create_treemap(child['code'], child['name'])] else: if float(child['value']) != 0: treemap['children'] += [{'name': child['name'], 'value': child['value']}] return treemap i = create_treemap('112', name='Taxas') i imp_node = {} imp_node['name'] = 'Impostos' imp_node['children'] = [] fapi_request_data treemap_taxas = {} treemap_taxas['name'] = 'Taxas' treemap_taxa['children'] = [] tax_node = {} tax_node['name'] = 'Taxas' tax_node['children'] = [] for tax_data in taxas_dict['data']: value, name = tax_data.values() tax_node['children'].append({'name': name, 'value': value}) treemap['children'] += [imp_node, tax_node] out = json.dumps(i, encoding="utf-8", indent=2) f = open('taxas.json', 'w+') f.write(out) f.close() !cat receitas.json json.dumps(i, encoding="utf-8", indent=2) s = '1121...' s.count('.')