You need to sign in to do that
Don't have an account?
Dionatan Vieira
Malformed JSON: Expected '[' at the beginning of List/Set after update a API file
I have an apex class that converts a JSON response to a list.
After update a ear file from my API, the JSON response now has new fields.
Before the update, the class works perfectly, but with the new response, I get the error "Malformed JSON: Expected '[' at the beginning of List/Set".
I tried to include the new fields at my class, but the error persists.
The class:
The old JSON response:
The new JSON response:
I really can't understand why the new response with the same format but with new fields don't works.
After update a ear file from my API, the JSON response now has new fields.
Before the update, the class works perfectly, but with the new response, I get the error "Malformed JSON: Expected '[' at the beginning of List/Set".
I tried to include the new fields at my class, but the error persists.
The class:
public class TESTE { private static final TESTE instance = new TESTE(); private static final ConfigIntegracao__c config = ConfigIntegracao__c.getInstance(); public static TESTE getInstance(){ //new Map<Grupo__c, String> return instance; } public class lista{ public String idGeral{get;set;} public String dtAtz{get;set;} public String cdGrupoSimilarItem{get;set;} public List<Itens> itens{get;set;} public List<Codigos> codigos{get;set;} } public static List<Product2> ProdutoConstruct(List<Itens> produtos){ List<Product2> ret = new List<Product2>(); for(Itens produto: produtos){ Product2 pt = new Product2(); pt.IdGeral__c = produto.idItem; pt.CodigoProduto__c = produto.cdItem; pt.CodigoItem__c = produto.cdItem; pt.Name = produto.nmItem; pt.NomeReduzido__c = produto.nmReduzido; if (produto.flAtivo.tolowercase() == 's'){ pt.Ativo__c = true; }else{ pt.Ativo__c = false; } if (produto.cdGrupo != null) { Grupo__c grupo = new Grupo__c(CodigoGrupo__c = produto.cdGrupo); pt.Grupo__r = grupo; } if (produto.cdGrupo != null && produto.cdSubgrupo != null) { Subgrupo__c subgrupo = new Subgrupo__c(CodigoSubGrupo__c = '' + produto.cdGrupo + produto.cdSubgrupo); pt.Subgrupo__r = subgrupo; } pt.UnidadeCompra__c = produto.unMedCompra; pt.UnidadeArmazenagem__c = produto.unMedArmazenagem; pt.UnidadeEstoque__c = produto.unMedEstoque; pt.UnidadeVenda__c = produto.unMedVenda; pt.CodigoLinhaProduto__c = produto.cdLinhaProduto != null ? Decimal.valueOf(produto.cdLinhaProduto) : null; pt.CodigoOriginal__c = produto.cdOriginal; if (produto.cdMarca != null) { Marca__c marca = new Marca__c(CodigoMarca__c = produto.cdMarca); pt.Marca__r = marca; } pt.CodigoFabricante__c = produto.cdFabricante; if(produto.qtAtual != null && produto.qtAtual != ''){ pt.Quantidade__c = Decimal.valueOf(produto.qtAtual); } // não achei campo no Product2 para bater com: fichaTecnica pt.QuantidadeImagens__c = produto.qtImagem; pt.Integrado__c = true; pt.CurvaABCImpave__c = produto.curvaAbc; pt.CurvaABCFornecedor__c = produto.curvaAbcForn; //NOVOS PARÂMETROS pt.nvpreco__c = produto.preco; pt.nvflPromocao__c = produto.flPromocao; pt.nvpcMaxDescontoAut__c = produto.pcMaxDescontoAut; pt.nvipi__c = produto.ipi; pt.nvst__c = produto.st; pt.nvipiSt__c = produto.ipiSt; pt.nvcstOrigem__c = produto.cstOrigem; pt.nvobs__c = produto.obs; pt.nvobsConferenciaSaida__c = produto.obsConferenciaSaida; pt.nvobsEmbalagem__c = produto.obsEmbalagem; pt.nvobsCompras__c = produto.obsCompras; pt.nvobsFichaTecnica__c = produto.obsFichaTecnica; pt.nvnmMarca__c = produto.nmMarca; pt.nvestoqueDisponivel__c = produto.estoqueDisponivel; pt.nvtemSimilares__c = produto.temSimilares; ret.add(pt); } return ret; } public class Itens { @AuraEnabled public String preco{get;set;} @AuraEnabled public String flPromocao{get;set;} @AuraEnabled public String pcMaxDescontoAut{get;set;} @AuraEnabled public String ipi{get;set;} @AuraEnabled public String st{get;set;} @AuraEnabled public String ipiSt{get;set;} @AuraEnabled public String idItem{get;set;} @AuraEnabled public String cdItem{get;set;} @AuraEnabled public String nmItem{get;set;} @AuraEnabled public String nmReduzido{get;set;} @AuraEnabled public String curvaAbc{get;set;} @AuraEnabled public String curvaAbcForn{get;set;} @AuraEnabled public String flAtivo{get;set;} @AuraEnabled public String cdGrupo{get;set;} @AuraEnabled public Integer cdSubgrupo{get;set;} @AuraEnabled public String unMedCompra{get;set;} @AuraEnabled public String unMedArmazenagem{get;set;} @AuraEnabled public String unMedEstoque{get;set;} @AuraEnabled public String unMedVenda{get;set;} @AuraEnabled public Integer cdLinhaProduto{get;set;} @AuraEnabled public String cdOriginal{get;set;} @AuraEnabled public String cdMarca{get;set;} @AuraEnabled public String cdFabricante{get;set;} @AuraEnabled public String fichaTecnica{get;set;} @AuraEnabled public String qtAtual{get;set;} @AuraEnabled public String cstOrigem{get;set;} @AuraEnabled public String obs{get;set;} @AuraEnabled public String obsConferenciaSaida{get;set;} @AuraEnabled public String obsEmbalagem{get;set;} @AuraEnabled public String obsCompras{get;set;} @AuraEnabled public String obsFichaTecnica{get;set;} @AuraEnabled public String nmMarca{get;set;} @AuraEnabled public String estoqueDisponivel{get;set;} @AuraEnabled public String temSimilares{get;set;} @AuraEnabled public Integer qtImagem{get;set;} } public class Codigos { @AuraEnabled public String codigo{get;set;} @AuraEnabled public String descricao{get;set;} } @AuraEnabled public static List<Lista> GetSimilarById (String id, String chave){ // String chave = null; List<Account> ret = new List<Account> (); List<Lista> retorno = new List<Lista>(); // chave = IntegracaoUtils.GetAutorization(); System.debug('INICIO'); if(chave != null){ Http h = new Http(); HttpRequest req = new HttpRequest(); URL baseUrl = URL.getSalesforceBaseUrl(); req.setEndpoint(config.EndPoint__c+'/Similares/getByItem?item='+id); req.setMethod('GET'); req.setTimeout(120000); req.setHeader('authToken', chave); try{ HttpResponse res = h.send(req); System.debug(res.getBody()); String retflex = res.getBody(); retorno = (List<Lista>)System.JSON.deserialize(retflex, List<Lista>.class); return retorno; } catch(exception e){ System.debug('Erro: ' + e); return null; } } else{ System.debug('Chave Inválida '+chave); // return null; } return retorno; } }
The old JSON response:
[ { "idGeral": 1000021541136, "dtAtz": "2014-08-07 15:42:40.0", "cdGrupoSimilarItem": 1466, "itens": [ { "idItem": 1027, "cdItem": "10070300", "nmItem": "JUNTA CABEÇOTE H100/L200/HR/K2500 2.5 8V FIBRA 1.55MM 4 PIC", "nmReduzido": "JUNTA CABEÇOTE H100 2.5 8V 97/... L200 2.5 8V 92/../02 / HR/K2500 2.5 8V 05/../12 FIBRA 1.55MM 4 PIC", "curvaAbc": "A", "curvaAbcForn": "C", "flAtivo": "S", "cdGrupo": 110, "cdSubgrupo": 1, "unMedCompra": "UNID", "unMedArmazenagem": "PC", "unMedEstoque": "PC", "unMedVenda": "PC", "cdLinhaProduto": 4, "cdOriginal": "", "cdMarca": 1, "cdFabricante": "10070300", "fichaTecnica": "\rDIAMETRO PISTÃO 92,50 MM\rESPESSURA 1,55 MM\rALTURA DO PISTÃO A PARTIR DE 0,680 ATÉ 0,744 MM" } ], "codigos": [ { "codigo": "2231142855", "descricao": "ORIGINAL" } ] } ]
The new JSON response:
[ { "idGeral": 1000021541136, "dtAtz": "2014-08-07 15:42:40.0", "cdGrupoSimilarItem": 1466, "itens": [ { "preco": 0, "flPromocao": false, "pcMaxDescontoAut": 0, "ipi": 0, "st": 0, "ipiSt": 0, "idItem": 1027, "cdItem": "10070300", "nmItem": "JUNTA CABEÇOTE H100/L200/HR/K2500 2.5 8V FIBRA 1.55MM 4 PIC", "nmReduzido": "JUNTA CABEÇOTE H100 2.5 8V 97/... L200 2.5 8V 92/../02 / HR/K2500 2.5 8V 05/../12 FIBRA 1.55MM 4 PIC", "curvaAbc": "A", "curvaAbcForn": "C", "flAtivo": "S", "cdGrupo": 110, "cdSubgrupo": 1, "unMedCompra": "UNID", "unMedArmazenagem": "PC", "unMedEstoque": "PC", "unMedVenda": "PC", "cdLinhaProduto": 4, "cdOriginal": "", "cdMarca": 1, "cdFabricante": "10070300", "fichaTecnica": " DIAMETRO PISTÃO 92,50 MM ESPESSURA 1,55 MM ALTURA DO PISTÃO A PARTIR DE 0,680 ATÉ 0,744 MM ", "qtAtual": 12.0, "cstOrigem": "", "obs": "Teste", "obsConferenciaSaida": "", "obsEmbalagem": "", "obsCompras": "\r\n", "obsFichaTecnica": " DIAMETRO PISTÃO 92,50 MM ESPESSURA 1,55 MM ALTURA DO PISTÃO A PARTIR DE 0,680 ATÉ 0,744 MM ", "nmMarca": "AJUSA", "estoqueDisponivel": true, "temSimilares": true } ], "codigos": [ { "codigo": "2231142855", "descricao": "ORIGINAL" } ] } ]
I really can't understand why the new response with the same format but with new fields don't works.
The error message "Malformed JSON: Expected '[' at the beginning of List/Set" usually indicates that the JSON string is not correctly formatted. It is not related to the class that converts the JSON response to a list.
When you updated the ear file from your API, the JSON response now has new fields. It is possible that the new fields are not in the correct format or contain an error, causing the JSON to be invalid.
To troubleshoot this issue, you can check the JSON response using an online JSON validator tool to see if there are any formatting errors. If you confirm that the JSON is valid, you can update the Apex class to include the new fields.
You can add the new fields to the Apex class by creating new variables in the Itens class and the lista class. For example, if the new field in the JSON response is called newField, you would add the following code:
You would also need to update the method ProdutoConstruct to populate the new field:
Make sure you save your changes, and try again to see if the error persists.
Hope the above information helps !
Thank you.