Solution
import sys
# Reading the json file
try:
with open("myjsonfile_list_of_dicts.json", "r") as read_content:
ls_plano: dict = json.load(read_content)
except (FileNotFoundError, PermissionError, OSError, ValueError) as e:
print(f"Error opening the file: {e}")
sys.exit()
# Parsing
try:
resp = []
if ls_plano is not None:
for plano in ls_plano:
temp_plano = {"inPlanoPersonalizado": plano.get("inPlanoPersonalizado"),
"inSelecionado": plano.get("inSelecionado")}
if plano.get("lsChassi"):
temp_plano["lsChassi"] = self.__map_ls_chassi(plano.get("lsChassi", []))
if plano.get("lsTipoObjetoSegurado"):
temp_plano["lsTipoObjetoSegurado"] = self.__map_ls_tipo_ob_segurado(
plano.get("lsTipoObjetoSegurado")
)
if plano.get("lsComissao"):
temp_plano["lsComissao"] = self.__map_ls_comissao(plano.get("lsComissao", []))
if plano.get("lsParcela"):
temp_plano["lsParcela"] = self.__map_ls_items(plano.get("lsParcela", []))
temp_plano["nmIdentificadorPlano"] = plano.get("nmIdentificadorPlano")
temp_plano["nmPlano"] = plano.get("nmPlano")
temp_plano["nrPlano"] = plano.get("nrPlano")
temp_plano["vlAdicionalFracionamento"] = plano.get("vlAdicionalFracionamento")
temp_plano["vlAssistenciaFacultativa"] = plano.get("vlAssistenciaFacultativa")
temp_plano["vlCobranca"] = plano.get("vlCobranca")
temp_plano["vlComercial"] = plano.get("vlComercial")
temp_plano["vlIof"] = plano.get("vlIof")
temp_plano["vlPremioLiquido"] = plano.get("vlPremioLiquido")
temp_plano["vlPremioNet"] = plano.get("vlPremioNet")
temp_plano["vlPremioTarifa"] = plano.get("vlPremioTarifa")
temp_plano["vlPremioTotal"] = plano.get("vlPremioTotal")
temp_plano["vlTotalComissao"] = plano.get("vlTotalComissao")
temp_plano["vlTotalDesconto"] = plano.get("vlTotalDesconto")
resp.append(temp_plano)
return resp
except (KeyError, TypeError) as e:
print(f"Error parsing the json file: {e}")