0

I have seen some examples over the internet to parse the JSON, but still I'm unable to parse and store the values into a object for the Patch operation.

/services/apexrest/id/00hjkl00000kl00

below is the sample JSON

{
 "Details":{
  "SSN"      : "1234",
  "FULLSSN"  : "12345678"
}
}

This is the Apex class that I wrote

@RestResource(urlMapping='/id/*')
Global class Restservice{

@Httppatch
global static String doput() {

RestRequest req = RestContext.request;
RestResponse res = RestContext.response;

 Id leadid = req.requestURI.substring(req.requestURI.LastIndexOf('/')+1);
 Lead lead1 = [SELECT ID, isConverted FROM Lead WHERE id =: leadid];
 if(lead1.isconverted)
    {
   Blob jsonBlob = req.requestBody;
   String jsonString = jsonBlob.toString();

//I need to update the SSN and FULLSSN (From JSON) values in to this existing lead 

     return 'Success';   
    }
 else
  return 'Failure';
}


    public class Details {
        public String SSN;
        public String FULLSSN;
    }

}

Any help is much appreciated. Thanks!

1

First of all your endpoint and URL mapping for your web service don't match:

/services/apexrest/value/00hjkl00000kl00 should be /services/apexrest/id/00hjkl00000kl00 as per your example code.

Also your wrapper class name doesn't match the JSON example code so you can't directly serialize the JSON. Either change the JSON Details to Loanoptions or change the class name to Details.

Once all that is fixed you can simply take the JSON as a parameter in your method which will automatically serialize it into an object instance of your Loanoptions class. Let's say we've changed the JSON structure and it's now:

{
     "Loanoptions":{
      "SSN"      : "1234",
      "FULLSSN"  : "12345678"
    }
}

You can just simply do:

global static String doput(Loanoptions loanOptionsRecord) 
{
    // your existing code, then:

    lead1.SSN__c = loanOptionsRecord.SSN;
    lead1.Full_SSN__c = loanOptionsRecord.FULLSSN;
    update lead1;
}

UPDATE:

Alternatively, if you have different input occasionally, you can use the JSON.deserializeUntyped(), and get only certain values from the JSON string:

global static String doput() 
{
    // your existing code, then:
    Map <String, Object> resultObject = (Map <String, Object>)JSON.deserializeUntyped(response.getBody());
    Map <String, Object> detailsObject = (Map <String, Object>)resultObject.get('Details');

    lead1.SSN__c = (String)detailsObject.get('SSN');
    lead1.Full_SSN__c = (String)detailsObject.get('FULLSSN');
    update lead1;
}
  • Boris, I updated the URL sorry about that. I still don't understand how are the values being deserialized in the above code? – cartman Sep 29 '15 at 3:25
  • 1
    Salesforce performs implicit de-serialisation if you add a parameter to you input get or post method. This is helpful in case your input data is expected in a single format. If you are expecting input in different format every time, you can perform de-serialisation yourself by calling JSON.deserialize method from JSON class – manjit5190 Sep 29 '15 at 3:50
  • @manjit_singh: I'm looking for patch operation, and I don't think the implicit de serialization would work. – cartman Sep 29 '15 at 4:01
  • I believe it will work. – manjit5190 Sep 29 '15 at 4:12
  • I've updated my answer including an alternative way of how you can get only certain values from the JSON string. – Boris Bachovski Sep 29 '15 at 4:14
0

First you should check your json for Malformed JSON

Use the following json beautifier

https://jsonformatter.curiousconcept.com/

Also you can use following json2apexengine to parse your json in Apex Class

http://json2apexengine.somee.com/

use following method to deserialize your json

Decimal n = (Decimal)JSON.deserialize(endpoint, Decimal.class);

Decimal is your wrapper class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.