0

I am making a GET callout to the URL: https://th-superbadge-apex.herokuapp.com/equipment?_ga=2.245262661.1986105727.1612691455-984520333.1610882684. In the URL, the id field starts with _id which I cant use because I will get compile time error. How do I convert it to id and use it in my code?

public with sharing class WarehouseCalloutService implements Queueable, Database.AllowsCallouts{

      private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment?_ga=2.245262661.1986105727.1612691455-984520333.1610882684';
        
            public Integer maintenanceperiod{get;set;}
            public String name{get;set;}
            public Integer lifespan{get;set;}
            public Integer quantity{get;set;}
            public Integer cost{get;set;}
            public Boolean replacement{get;set;}
            public String sku{get;set;}
            public String  id{get;set;}
        
        public void execute(QueueableContext qc) {
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(WAREHOUSE_URL);
            request.setMethod('GET');
            HttpResponse response = http.send(request);
            
            
            // If the request is successful, parse the JSON response.
            if (response.getStatusCode() == 200) {
                // Deserialize the JSON string into collections of primitive data types.
               List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
                system.debug(results);
     
            }
        }}
2
  • Have you tried casting to String as you iterate over the List<Object> results? Im not sure why you would get a compile error, it might be more helpful for you to include the code which caused a compile error along with the error message itself.
    – TSmith
    Feb 8 at 15:40
  • When I declare public string _id I get a compile error. Underscore is not allowed as the first character. Sorry, I’m not in front of my computer now. Feb 8 at 15:41
1

Variable naming is fine here. It seems you are confused with how you would extract the _id value from the deserialized response. Here is a simple example of outputting each Id found in the deserialized response through a System.debug().

....

// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
    
    for (Object result : results) {
        Map<String, Object> resultMap = (Map<String, Object>) result;
        System.debug('Id: ' + (String) resultMap.get('_id'));
    }
}

....

The idea would then be that you safely construct the data structures you need in memory or in the database as you loop through each result.

1
  • Thank you. What’s the purpose of the casting of map in the 2nd line after for loop? Can you explain that line please? Feb 9 at 2:06
0

I had the same problem when solving Apex Specialist superbadge(when calling https://th-superbadge-apex.herokuapp.com/equipment). I found the solution in the answer for different(not related to this question) issue: https://salesforce.stackexchange.com/a/174391

It is possible to call replace on the response body to change '_id' to 'id'. For example:

(List<cEquipment>.class)JSON.deserialize(response.getBody().replace('"_id"','"id"'),List<cEquipment>.class)

cEquipment declared as

public class cEquipment {
  public String id {get; set;}
  /* other fields
  ... 
  */
}

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.