function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Célio XavierCélio Xavier 

Fatal Erro: Malformed JSON: Expected '{' at the beginning of object


I need help please, how do I make a template class for this Json, I've tried several ways but I'm getting the message that the Json is malformed.
 
[
    {
        "tipo": "PECÚLIO POR MORTE - RISCO",
        "beneficiarios": [
            {
                "idade": 60,
                "nascimento": "0000-00-00",
                "parentesco": "PAIS",
                "percentualRateio": 50,
                "cpf": "12312312345",
                "nome": "L***MASC***",
                "id": 3449190,
                "sexo": "FEMININO"
            },
            {
                "idade": 38,
                "nascimento": "0000-01-00",
                "parentesco": "IRMÃO",
                "percentualRateio": 50,
                "cpf": "12312312365",
                "nome": "S***MASC***",
                "id": 3449188,
                "sexo": "MASCULINO"
            }
        ],
        "valorBeneficio": 46100.83,
        "valorPagamento": 79.13,
        "status": "SUSPENSO"
    },
    {
        "tipo": "RENDA MENSAL VITALÍCIA - SOBREVIVENCIA",
        "idadeSaida": 65,
        "beneficiarios": [
            {
                "idade": 38,
                "nascimento": "0000-01-00",
                "parentesco": "IRMÃO",
                "percentualRateio": 50,
                "cpf": "12312312378",
                "nome": "S***MASC***",
                "id": 3449188,
                "sexo": "MASCULINO"
            },
            {
                "idade": 63,
                "nascimento": "0000-07-00",
                "parentesco": "PAIS",
                "percentualRateio": 50,
                "cpf": "12312312378",
                "nome": "L***MASC***",
                "id": 3449190,
                "sexo": "FEMININO"
            }
        ],
        "valorBeneficio": 0,
        "dataSaida": "2053-10-23",
        "valorPagamento": 0,
        "status": "SUSPENSO"
    }
]

My class:
 
public with sharing class ConsultationBeneficiaryModels {
    
    public class JSON2Apex {
	    @AuraEnabled public String tipo;	    
	    @AuraEnabled public Double valorBeneficio;
        public List<Beneficiarios> beneficiarios;
        @AuraEnabled public Double valorPagamento;
        @AuraEnabled public String status;
        @AuraEnabled public Integer idadeSaida;
        @AuraEnabled public String dataSaida;
    }
	public class Beneficiarios {
		@AuraEnabled public Integer idade;
		@AuraEnabled public String nascimento;
		@AuraEnabled public String parentesco;
		@AuraEnabled public Integer percentualRateio;
		@AuraEnabled public String cpf;
		@AuraEnabled public String nome;
		@AuraEnabled public Integer id;
		@AuraEnabled public String sexo;
    }
}

Message error:

FATAL_ERROR System.JSONException: Malformed JSON: Expected '{' at the beginning of object
mukesh guptamukesh gupta
HI Celio,

Please use below Tested Code:-
 
public class JSON2Apex {

	public String tipo;
	public List<Beneficiarios> beneficiarios;
	public Double valorBeneficio;
	public Double valorPagamento;
	public String status;
	public Integer idadeSaida;
	public String dataSaida;

	public class Beneficiarios {
		public Integer idade;
		public String nascimento;
		public String parentesco;
		public Integer percentualRateio;
		public String cpf;
		public String nome;
		public Integer id;
		public String sexo;
	}

	
	public static List<JSON2Apex> parse(String json) {
		return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);
	}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Célio XavierCélio Xavier
Guys, I was able to solve without changing Json's class, i used this code:
 
List<ConsultationBeneficiaryModels.JSON2Apex> results = 
  (List<ConsultationBeneficiaryModels.JSON2Apex>)
  JSON.deserialize(
    jsonString, 
    List<ConsultationBeneficiaryModels.JSON2Apex>.class
  );
I was doing this:
 
ConsultationBeneficiaryModels.JSON2Apex response = (ConsultationBeneficiaryModels.JSON2Apex) System.JSON.deserialize(resp.getBody(), ConsultationBeneficiaryModels.JSON2Apex.class);