Skip to content

Instantly share code, notes, and snippets.

@lpirola
Last active February 14, 2025 14:52
Show Gist options
  • Save lpirola/6057975ed5df715926b1c5ce5c5ce1a8 to your computer and use it in GitHub Desktop.
Save lpirola/6057975ed5df715926b1c5ce5c5ce1a8 to your computer and use it in GitHub Desktop.
import requests
import json
# URL da API do IBGE
url = "https://servicodados.ibge.gov.br/api/v2/cnae/subclasses"
# Faz a requisição HTTP para obter os dados
response = requests.get(url)
# Verifica se a requisição foi bem-sucedida
if response.status_code == 200:
# Carrega o JSON da resposta
data = response.json()
# Filtra apenas os campos 'id' e 'descricao' de cada item
filtered_data = [{"id": item["id"], "descricao": item["descricao"]} for item in data]
# Exibe os dados filtrados
print(filtered_data)
# (Opcional) Salva os dados filtrados em um novo arquivo JSON
with open('filtered_subclasses.json', 'w', encoding='utf-8') as file:
json.dump(filtered_data, file, ensure_ascii=False, indent=4)
print("Dados filtrados salvos em 'filtered_subclasses.json'")
else:
print(f"Erro ao acessar a API. Código de status: {response.status_code}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment