0

Im trying to get the Id and Name from JSON Body that I requested, and put the value into custom object

{
"id": "ab24ddb8-f1ef-4cca-ae72-9f20f5975332", <-- THIS
"name": "App Dev Account", <-- AND THIS
"division": {
    "id": "ac2ef38b-9aab-40e9-ba47-10e121cd1d81",
    "name": "",
    "selfUri": "/api/v2/authorization/divisions/ac2ef38b-9aab-40e9-ba47-10e121cd1d81"
}

My parsing code return id value, but not the 1st id value..

my code return

"id": "ac2ef38b-9aab-40e9-ba47-10e121cd1d81" instead of

this "id": "ab24ddb8-f1ef-4cca-ae72-9f20f5975332"

this is my apex class :

public class Purecloud_TestGet {
//@future (callout=true)  // future method needed to run callouts from Triggers
static public void getTest(){
    String url = Label.API_Purecloud+'api/v2/users/me?id=ab24ddb8-f1ef-4cca-ae72-9f20f5975332';
    String clientid = '5a2cd181-0181-43a5-8334-6a1b6ad2551f';
    String clientSecret = 'z3FcATf3Lr3gwoqqf9ldYnc53Cuaaw_VblxAJdJn-EY';
    String token = '3zImrHaYyD79dK8rjOtwrBhAqm2-ReX6z4u55Y1ez8xVNu1ASfYPwoXadiDzzDveDd_GvpwxlLPSBqEau9Q2ag';
    system.debug(LoggingLevel.DEBUG,'*url*'+url);

    List<CTI_Test_Purecloud__c> ctilisttoinsert = new List<CTI_Test_Purecloud__c>();
    String purecloudid = '';
    String Nama = '';
    String email = '';

Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');
    req.setTimeout(120000);
    req.setHeader('Authorization', 'Bearer '+ token);
    req.setHeader('content-type', 'application/json');

    try{
        HttpResponse res = new HttpResponse();
        res = h.send(req);
            JSONParser parser;
            parser = JSON.createParser(res.getBody());
                while (parser.nextToken() != null) {
                    if(parser.getCurrentToken() == JSONToken.FIELD_NAME){
                        if (parser.getText() == 'id'){
                            parser.nextToken();
                            purecloudid = parser.getText();
                        }
                        else if (parser.getText() == 'name'){
                            parser.nextToken();
                            Nama = parser.getText();
                        } 
                        else if (parser.getText() == 'email'){
                            parser.nextToken();
                            Email = parser.getText();
                        }
                    }

                }
        CTI_Test_Purecloud__c insertcti = new CTI_Test_Purecloud__c();
        insertcti.Purecloud_User_Id__c = purecloudid;
        insertcti.Name = Nama;
        insertcti.Email__c = Email;
        ctilisttoinsert.add(insertcti);
    }
    catch(exception e){
        system.debug('*error*'+e);
    }
    insert ctilisttoinsert;

}

}

2

Well, technically it is behaving as you have programmed. The issue I see here is when you receive multiple items in JSON, you will only get last ones because the previous value of id is overridden by last value in the JSON response due to for loop.

If your output response structure is static, I suggest to generate a deserializing class using http://json2apex.herokuapp.com/ tool and use JSON.deserialize method.

2

Makes sense. Your id value is being overwritten because, when your while loop gets inside the division object in your JSON, parser.getText() is going to equal "id" for the second time.

Whenever possible, I find it's easier to use the apex JSON.deserialize method than to parse it out "manually". All you have to do is define classes that match the keys in your JSON object, and use

JSON.deserialize(jsonString, apexType); 

In this case, something like the following should work:

// Define classes to deserialize your JSON into

public class CtiWrapper {
    String id;
    String name;
    String email;

    // If you need info from the division, uncomment this line 
    // and the Division class below. If not, the deserialize
    // method will ignore any keys that aren't modeled in 
    // the class, so...no big deal!
    //list<Division> division;

}
/*
public class Division {
    String id;
    String name;
    String selfUri;

}
*/
...
...
...
try{
    HttpResponse res = new HttpResponse();
    res = h.send(req);

    CtiWrapper wrapper = (CtiWrapper) JSON.deserialize(res, CtiWrapper.class);

    CTI_Test_Purecloud__c insertcti = new CTI_Test_Purecloud__c();
    insertcti.Purecloud_User_Id__c = wrapper.id;
    insertcti.Name = wrapper.name;
    insertcti.Email__c = wrapper.email;

    // if you needed info from the Division, you could use something like:
    // wrapper.division[0].selfUri;

    ctilisttoinsert.add(insertcti);

} catch(exception e){
    system.debug('*error*'+e);
}

insert ctilisttoinsert;
  • Were you able to make this work? – Shane Steinfeld Sep 30 '18 at 19:54

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.