1

I am having a simple JSON as like below

{"Stagename":"Prospecting","OppName":"MM - Opportunity","CloseDate":"2022-10-08","Phone":"11111","Lastname":"M","Firstname":"M","Name":"MM"}

And trying to parse using the below class

public with sharing class ParseQuoteRequest {
    public String Stagename;    //Prospecting
    public String OppName;  //MM - Opportunity
    public String CloseDate;    //2022-10-08
    public String Phone;    //11111
    public String Lastname; //M
    public String Firstname;    //M
    public String Email;    //[email protected]
    public String Name; //MM
    public static ParseQuoteRequest parse(String json){
        return (ParseQuoteRequest) System.JSON.deserialize(json, ParseQuoteRequest.class);
    }
}

and here is how I am calling from my Restresource like below

  ParseQuoteRequest parsedRequest= ParseQuoteRequest.parse(req.requestBody.toString());

and I am getting this error

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

and I tried without desrialize also by this way

Map<String, Object> responseMap =   (Map<String, Object>) JSON.deserializeUntyped(req.requestBody.toString());

then also getting the same error

and even tried like this

[{"Stagename":"Prospecting","OppName":"MM - Opportunity","CloseDate":"2022-10-08","Phone":"11111","Lastname":"M","Firstname":"M","Name":"MM"}]


public with sharing class ParseQuoteRequest {
    public String Stagename;    //Prospecting
    public String OppName;  //MM - Opportunity
    public String CloseDate;    //2022-10-08
    public String Phone;    //11111
    public String Lastname; //M
    public String Firstname;    //M
    public String Email;    //[email protected]
    public String Name; //MM
    public static ParseQuoteRequest parse(String json){
        return (List<ParseQuoteRequest>) System.JSON.deserialize(json, List<ParseQuoteRequest>.class);
    }
}


5
  • Map<String, Object> request = new Map<String, Object>(); request.put('Stagename', 'Prospecting'); request.put('OppName', 'MM - Opportunity'); request.put('CloseDate', '2022-10-08'); request.put('Phone', '11111'); request.put('Lastname', 'M'); request.put('Firstname', 'F'); request.put('Email', '[email protected]'); ParseQuoteRequest req = ParseQuoteRequest.parse(JSON.serialize(request)); System.debug(req); I tried this and works perfect for me
    – Ysr Shk
    Commented Jul 8, 2022 at 12:49
  • 1
    Copy/pasting the JSON that you've provided yields no issues for me when I deserialize untyped, and I haven't been able to get the same error as you when I try making the JSON invalid using the common issues people tend to run into (double escaping, list instead of object, JSON embedded in html, etc...). Are you certain that your request body is returning JSON and nothing else? Is it using UTF-8? If you look at the hex representation of the data you're getting, is the first value 0x7B?
    – Derek F
    Commented Jul 8, 2022 at 13:03
  • @DerekF , Thanks for the lead. I am sending the JSON to Node JS application and from there I am sending to salesforce. I used JSON.Stringify(json) which added extra escaping and caused the issue. after removing JSON.stringify now lloking good.
    – SFDC buddy
    Commented Jul 8, 2022 at 13:15
  • 2
    @SFDCbuddy You should add that as an answer (it's perfectly fine to answer your own questions here) so it can help other people (in the future) with the same issue. If you can include the string with the extra escaping in it, that'd be helpful for others as well.
    – Derek F
    Commented Jul 8, 2022 at 13:23
  • @DerekF Thanks. Will do
    – SFDC buddy
    Commented Jul 9, 2022 at 4:45

1 Answer 1

1

My original requirment is I want to create a CPQ quote and Quote line from community site. But since salesforce is having some restriction that CPQ objects cant be accessed through Guest user license as a work around I created one heroku node JS application. So from my community site Apex class I am hitting the node JS application(sending JSON) and Node JS is again pushing the data to salesforce

Community Apex class code

Response resp= new Response(quoteDetails);
HttpRequest request = new HttpRequest();
request.setBody(JSON.serializePretty(resp));
//{"Stagename":"Prospecting","OppName":"MM - Opportunity","CloseDate":"2022-10-08","Phone":"11111","Lastname":"M","Firstname":"M","Name":"MM"}

and in node JS I am doing like this

restService.post("/createQuote", function (req, mainres) {  

    try{
       var conn = new jsforce.Connection({
       loginUrl : config.salesforceLoginURL
       });
config.salesforcePassword+config.salesforceSecurityToken, function(err, userInfo) {

conn.apex.post("/createteslaquote/",req.body, function(res,respo) {

Before in this place I am using like this

conn.apex.post("/createteslaquote/",JSON.Stringify(req.body), function(res,respo) {

and when i removed this JSON.Stringify , things are fine

conn.apex.post("/createteslaquote/",req.body, function(res,respo) {

as like @DerekF mentioned JSON.Stringify is adding extra layer of serialization

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .