0

i am trying to parse a response which is a json object that has JSON array, what I need is to get value in the specific object from the array, inside "results" I'd like to get value from key "id" which is 5438.

this question is similar to mine but it does not really solve my problem

my GET method response is

{
    "results": [
        {
            "url": "https://propertypro.zendesk.com/api/v2/tickets/5438.json",
            "id": 5438,
            "external_id": null,
            "via": {
                "channel": "any_channel",
                "source": {
                    "from": {
                        "service_info": {
                            "supports_channelback": true,
                            "supports_clickthrough": false,
                            "registered_integration_service_name": "WhatsApp",
                            "registered_integration_service_external_id": "wh"
                        }
                    }
                }
            }
        }
    ]
} 

my full code

            // Set the GET request parameters
            String endpointGet = 'https://propertypro.zendesk.com/api/v2/search.json?'; // be sure this is configured in "Remote Site Settings"
            String resourceGetNoReplyWindow = 'query=type%3Aticket+tags%3Asend_whatsapp+status%3Cclosed';

            // Set the Methods
            String method_get = 'GET';

            // Set the credentials
            String username = 'some email'; 
            String password = 'Some password';

                // define transaction variables for 2 GET requests
                HttpRequest reqGetNrw = new HttpRequest();
                HttpResponse resGetNrw = new HttpResponse();
                Http hGetNrw = new Http();

                // Configure the 2 GET requests
                reqGetNrw.setEndpoint(endpointGet + resourceGetNoReplyWindow);
                reqGetNrw.setMethod(method_get);


                // Do the HTTP get request
                Blob headerValue = Blob.valueOf(username + ':' + password);
                String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);

                reqGetNrw.setHeader('Authorization', authorizationHeader);            
                reqGetNrw.setHeader('Content-Type', 'application/json');

                resGetNrw = hGetNrw.send(reqGetNrw);    

                                Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(resGetNrw.getBody());

                                List<Map<String, Object>> data = new List<Map<String, Object>>();

                                for(Object instance : (List<Object>)deserialized.get('results')){
                                    data.add((Map<String, Object>)instance);
                                }
                                    System.debug(data);
1

Your in the right way!

Here, after getting instance as a Map, just retrieve the needed value:

Map<String, Object> deserialized = (Map<String, Object>) JSON.deserializeUntyped(resGetNrw.getBody());

for (Object instance : (List<Object>) deserialized.get('results')) {
    Map<String, Object> data_i = (Map<String, Object>) instance;

    String id_i = (String) data_i.get('id');
    system.debug(id_i);
}
  • Hi Mariia, thanks for the quick response, your answer is what I'm looking for and I just need to convert the 'id' to string as its an integer type. thank you! – Adnan Zahry Sep 25 at 10:35
  • @AdnanZahry Or you just can leave it as Ier, for example, Integer id_i = (Integer) data_i.get('id'); – Mariia Illarionova Sep 25 at 10:43
  • ah ok, one final thing, for instance when debugging, I get 2 list [77]|DEBUG|5617 [77]|DEBUG|5606 and I need to convert them into string and assign the value to new variable with comma separated, what I should do here? – Adnan Zahry Sep 25 at 10:55
  • Create List<String>, add values to yjis list instead of debugging .add((String) id-i), then make a String ans = String.join(yourListName, ','); – Mariia Illarionova Sep 25 at 11:05

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.