You need to sign in to do that
Don't have an account?
Tulasiram Chippala
Malformed json error in REST class
Hi i am getting below error :
21:07:14:009 FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
Apex class:
@RestResource(urlMapping='/LeadManagement')
global with sharing class LeadManagement {
@HttpPost
global static LeadWrapper.ResposeWrappper LeadPost(){
RestRequest req = RestContext.request;
system.debug('----------hiiiiiiiii');
String reqresponse = req.requestBody.toString().replace('{"size":124,"totalSize":124,"done":true,"queryLocator":null,"entityTypeName":"LeadWrapper.RequestWrappper","records":','');
system.debug('---------'+reqresponse);
List<LeadWrapper.RequestWrappper> objRequest = (List<LeadWrapper.RequestWrappper>)JSON.deserialize(reqresponse,
List<LeadWrapper.RequestWrappper>.class);
/*LeadWrapper.RequestWrappper[] delegationMap = (LeadWrapper.RequestWrappper[])JSON.deserialize(req.requestBody.toString(), LeadWrapper.RequestWrappper[].class);*/
system.debug('----------'+objRequest);
// Checking the requested prameters are not null
List<Lead> insertLeads = new List<Lead>();
if(objRequest != null){
for(Lead ld:getLeadMapping(objRequest)){
insertLeads.add(ld);
}
Schema.SObjectField ftoken = Lead.Fields.External_Id__c;
Database.UpsertResult[] srList = Database.upsert(insertLeads, false);
/* for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) return new LeadWrapper.ResposeWrappper('Successfully inserted','');
} */
return new LeadWrapper.ResposeWrappper('Successfully inserted','');
}
else
return new LeadWrapper.ResposeWrappper('','No data to insert');
} //method
private static List<Lead> getLeadMapping(LeadWrapper.RequestWrappper[] objRequest){
List<Lead> lstLead = new List<Lead>();
for(LeadWrapper.RequestWrappper lst:objRequest){
Lead ld = new Lead(FirstName = lst.FirstName,
LastName =lst.LastName,
Company = lst.CompanyName,
Email = lst.Email,
Phone = lst.Phone,
Marketing_Rep_Name__c = lst.MarketingRepName,
LeadSource = lst.LeadSource,
Street = lst.Street,
City = lst.City,
State = lst.State,
Country = lst.Country,
External_Id__c = integer.valueOf(lst.External_Id));
lstLead.add(ld);
}
return lstLead;
}
} //class
Wrapper Class:
global class LeadWrapper {
global class RequestWrappper {
global String FirstName;
global String LastName;
global String CompanyName;
global String Email;
global String Phone;
global String MarketingRepName;
global String LeadSource;
global String Street;
global String City;
global String State;
global String Country;
global String External_Id;
}
global class ResposeWrappper{
global String SuccessMsg;
global String ErrorMsg;
global ResposeWrappper(String SuccessMsg, String ErrorMsg){
this.SuccessMsg = SuccessMsg;
this.ErrorMsg = ErrorMsg;
}
}
}
21:07:14:009 FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
Apex class:
@RestResource(urlMapping='/LeadManagement')
global with sharing class LeadManagement {
@HttpPost
global static LeadWrapper.ResposeWrappper LeadPost(){
RestRequest req = RestContext.request;
system.debug('----------hiiiiiiiii');
String reqresponse = req.requestBody.toString().replace('{"size":124,"totalSize":124,"done":true,"queryLocator":null,"entityTypeName":"LeadWrapper.RequestWrappper","records":','');
system.debug('---------'+reqresponse);
List<LeadWrapper.RequestWrappper> objRequest = (List<LeadWrapper.RequestWrappper>)JSON.deserialize(reqresponse,
List<LeadWrapper.RequestWrappper>.class);
/*LeadWrapper.RequestWrappper[] delegationMap = (LeadWrapper.RequestWrappper[])JSON.deserialize(req.requestBody.toString(), LeadWrapper.RequestWrappper[].class);*/
system.debug('----------'+objRequest);
// Checking the requested prameters are not null
List<Lead> insertLeads = new List<Lead>();
if(objRequest != null){
for(Lead ld:getLeadMapping(objRequest)){
insertLeads.add(ld);
}
Schema.SObjectField ftoken = Lead.Fields.External_Id__c;
Database.UpsertResult[] srList = Database.upsert(insertLeads, false);
/* for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) return new LeadWrapper.ResposeWrappper('Successfully inserted','');
} */
return new LeadWrapper.ResposeWrappper('Successfully inserted','');
}
else
return new LeadWrapper.ResposeWrappper('','No data to insert');
} //method
private static List<Lead> getLeadMapping(LeadWrapper.RequestWrappper[] objRequest){
List<Lead> lstLead = new List<Lead>();
for(LeadWrapper.RequestWrappper lst:objRequest){
Lead ld = new Lead(FirstName = lst.FirstName,
LastName =lst.LastName,
Company = lst.CompanyName,
Email = lst.Email,
Phone = lst.Phone,
Marketing_Rep_Name__c = lst.MarketingRepName,
LeadSource = lst.LeadSource,
Street = lst.Street,
City = lst.City,
State = lst.State,
Country = lst.Country,
External_Id__c = integer.valueOf(lst.External_Id));
lstLead.add(ld);
}
return lstLead;
}
} //class
Wrapper Class:
global class LeadWrapper {
global class RequestWrappper {
global String FirstName;
global String LastName;
global String CompanyName;
global String Email;
global String Phone;
global String MarketingRepName;
global String LeadSource;
global String Street;
global String City;
global String State;
global String Country;
global String External_Id;
}
global class ResposeWrappper{
global String SuccessMsg;
global String ErrorMsg;
global ResposeWrappper(String SuccessMsg, String ErrorMsg){
this.SuccessMsg = SuccessMsg;
this.ErrorMsg = ErrorMsg;
}
}
}