Trialhead Apex REST Callouts challenge https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_rest_callouts

Hi i am very new to salesforce and has statred learning through Trialhead. I am in the REST callouts section. I started it but i don't know how to get the response. i am not sure if i am going about this right. This is my first time even seeing a http service. please any help or guidance would be appreciated. i have pasted my code here .. I get an error that an object cannot be assigned to a string. if i change the type of result to object , the error goes away.I am not sure how to convert it back to a string.

public class AnimalLocator {

    public static String getAnimalNameById(Integer id){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        HttpResponse response = http.send(request);

        // check the response
      if (response.getStatusCode() == 200)
           String result = JSON.deserializeUntyped(response.getBody());

      }//end of if
            return null;
    }//end of method
    }//end of class
  • Thanks guys(@Ashwani and @Stygon) it fixed that error but opened me up to other errors. i will try to take out one by one and again do an update here.
    – Dino
    Mar 15, 2016 at 16:59
  • Hi Dino - if you have further problems then it would be good to raise these separately as questions. That way this question and answer are preserved to help someone else hitting the same issue as you.
    – Doug B
    Mar 15, 2016 at 17:19

3 Answers 3


When you use JSON.deserializeUntyped(Object) method you must type-cast it to expected type always. Assuming that you are expecting String type so, statement should be like:

String result = (String)JSON.deserializeUntyped(response.getBody());

Alternative but not always recommended

String result = String.valueOf( JSON.deserializeUntyped(response.getBody()) );

Another one which does not require even type casting:

String result = JSON.deserialize(response.getBody(), String.class);


Type-cast it into string

String result = (String)JSON.deserializeUntyped(response.getBody());

then it should avoid the compilation error. Basically what we are doing is that, we convert the result of JSON.deserializeUntyped() into a string and then store it inside the 'result' variable(which is already a string type reference).

String strjson = response.getbody();
Map<String,Object> newMapObj = Map<String,Object>JSON.deserializeUntyped(strjson );
  • 1
    Can you please preface your answer with some comments to help the OP? Mar 15, 2016 at 17:55
  • if you want to convert JSON response in string format then first you have to convert JSON string in Map<String,Object> format (please refer above synatx) then you can access Json response in String form as mention below :String strJson = '{"totalResults": 2, "startIndex":0, "TvPrice":[{"price":10.80,"cost":9.22,"gp":12},{"price":5.50,"cost":4.00,"gp":24}]}'; Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s); List<Object> pricing = (List<Object>) m.get('pricing'); for (Object o : pricing) {Map<String, Object> p = (Map<String, Object>) o; System.debug('>>> ' + p);} Mar 16, 2016 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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