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

Deserialize bit of JSON Response

Below is the JSON Response of External REST Service API. My requirement is to extract a piece of data from the response. I am unable to deserialize the piece of data which is in BOLD.

{"sports" :[{"name" :"tennis","id" :850,"uid" :"s:850","leagues" :[{"name" :"Association of Tennis Professionals","abbreviation" :"atp","id" :850,"groupId" :"atp","shortName" :"ATP","athletes" :[{"id" :760,"firstName" :"Jorge","lastName" :"Aguilar","fullName" :"Jorge Aguilar","displayName" :"Jorge Aguilar","shortName" :"J. Aguilar","links" :{"api" :{"athletes" :{"href" :""},"news" :{"href" :""},"notes" :{"href" :""}},"web" :{"athletes" :{"href" :""}},"mobile" :{"athletes" :{"href" :""}}}}]
}]}],"resultsOffset" :0,"resultsLimit" :50,"resultsCount" :1,"timestamp" :"2014-02-19T07:35:25Z","status" :"success"}
Sunil SharmaSunil Sharma

You can try like this ---

JSONParser parser = JSON.createParser(response.getBody());
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                 while (parser.nextToken() != null) {
                       if(parser.getCurrentToken() == JSONToken.START_OBJECT){
                           while (parser.nextToken() != null) {

                               if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'athletes') ){
                                   r = new subClass ();

                                   currObj = parser.getText();
                               if( ( parser.getCurrentToken() == JSONToken.START_OBJECT ) && ( currObj =='athletes' ) ) {

                                   while (parser.nextToken() != null) {
                                       if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'id')){

                                           r.f = parser.getText();

Sunil Sharma


   Can yo post your code once how you are connecting to third party system..
Sunil SharmaSunil Sharma

     You can try like this for consuming REST based web service (connecting to third party system)---

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        String url = 'end point url';
        string un = 'username';
        string pw = 'password';
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        Blob headerValue = Blob.valueOf(un+ ':' + pw);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);           
        req.setHeader('Authorization', authorizationHeader);
        HttpResponse res = h.send(req);
        System.debug('BODY: ' + res.getBody());
        //resp is a JSON string
        JSONParser parser = JSON.createParser(res.getBody());

Sunil Sharma
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Hi Nani,

Do you have a wrapper class for a properties mentioned in the JSON?

I not, create a wrapper class with the property of athletes should be a list of another wrapper class.  Because, atheletes is meaning  a list of values rgt?


For deserializing a JSON string,

public static String func(String activitiesJSON) 
List<WrapperClass1> ActivityID_List= (WrapperClass1) JSON.deserialize(activitiesJSON, WrapperClass1.class);
//Your logic here

public WrapperClass1{

Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator

@Kamatchi Devi

  I want to fetch the records of Accounts,contacts ,opportunities from third party system and i want to save the records into my objects where i want to declare my parameters can you give me sample program for this.PlZ