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
golugolu 

help with json parsing during callout

Hi All, 
I am able to create the get the values from json in my apex wrapper . But how do I iterate over it and store in task object. I want to store caller_number from the json in task records.Please help. Below is my code:
public class MyOperatorController {
    public static void Getlogs(){
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://developers.myoperator.co/search');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setHeader('Accept','application/json');
        request.setBody('{"token" : "cc5ceab6c02ef8699a82f8178b5c1596"}');
        HttpResponse response = http.send(request);
   
        system.debug('response------'+response.getBody());
        System.debug('STATUS:'+response.getStatus());
        
      JSON2Apex jsonval= JSON2Apex.parse(response.getBody());  
    system.debug('--jsonval---'+jsonval);
    
}

My Json response sample is :
{
    "status": "success",
    "code": 200,
    "data": {
        "total": 71,
        "max_score": null,
        "hits": [
            {
                "_index": "latestlogs",
                "_type": "logs",
                "user_id": "79b4935e0bc4e689bf2fe60ba2fed8bc7b914e4e",
                "_score": 16.230835,
                "_source": {
                    "allcaller_id": "79b4935e0bc4e689bf2fe60ba2fed8bc7b914e4e",
                    "source": "2",
                    "company_id": "5d08992bd0309594",
                    "caller_number": "+916289030819",
                    "caller_number_raw": "6289030819",
                    "_cm": "",
                    "start_time": 1561034490,
                    "_ms": 1561034490000,
                    "seconds": 45690,
                    "end_time": 1561034492,
                    "synced_time": 1561034510,
                    "_drs": 2,
                    "duration": "00:00:02",
                    "_drm": 0.03,
                    "type": 1,
                    "event": 2,
                    "status": 1,
                    "fileurl": "",
                    "filename": "",
                    "comments": null,
                    "additional_parameters": [
                        {
                            "ky": "unique_id",
                            "vl": "5d0b7f0ed0eca615"
                        },
                        {
                            "ky": "is_starred",
                            "vl": "0"
                        },
                        {
                            "ky": "view_type",
                            "vl": "2"
                        },
                        {
                            "ky": "is_commented",
                            "vl": "0"
                        },
                        {
                            "ky": "is_archived",
                            "vl": "0"
                        }
                    ],
                    "country": "91",
                    "state": "IN",
                    "log_details": [
                        {
                            "text": "",
                            "type": 1,
                            "duration": "00:00:02",
                            "start_time": 1561034490,
                            "end_time": 1561034492,
                            "action": "received",
                            "received_by": [
                                {
                                    "user_id": "5d08992bd3b04894",
                                    "name": "Manras Technology",
                                    "email": "rohit.aryan@manras.in",
                                    "extension": "10",
                                    "contact_number_raw": "+919041910069",
                                    "contact_number": "9041910069"
                                }
                            ]
                        }
                    ],
                    "_us": [
                        {
                            "ky": "5d08992bd3b04894",
                            "vl": "received"
                        }
                    ],
                    "_tc": [
                        {
                            "ye": "5d08992bd3b04894",
                            "yf": 2
                        }
                    ]
                }
            },
            {
                "_index": "latestlogs",
                "_type": "logs",
                "user_id": "646a535ebd481cb362c71ca1e8922ad698639d01",
                "_score": 16.142824,
                "_source": {
                    "allcaller_id": "646a535ebd481cb362c71ca1e8922ad698639d01",
                    "source": "2",
                    "company_id": "5d08992bd0309594",
                    "caller_number": "+915227188769",
                    "caller_number_raw": "5227188769",
                    "_cm": "",
                    "start_time": 1561029280,
                    "_ms": 1561029280000,
                    "seconds": 40480,
                    "end_time": 1561029280,
                    "synced_time": 1561034507,
                    "_drs": 0,
                    "duration": "00:00:00",
                    "_drm": 0,
                    "type": 1,
                    "event": 1,
                    "status": 1,
                    "fileurl": "",
                    "filename": "",
                    "comments": null,
                    "additional_parameters": [
                        {
                            "ky": "unique_id",
                            "vl": "5d0b7f0be1da7596"
                        },
                        {
                            "ky": "is_starred",
                            "vl": "0"
                        },
                        {
                            "ky": "view_type",
                            "vl": "2"
                        },
                        {
                            "ky": "is_commented",
                            "vl": "0"
                        },
                        {
                            "ky": "is_archived",
                            "vl": "0"
                        }
                    ],
                    "country": "91",
                    "state": "UP, IN",
                    "log_details": [
                        {
                            "text": "",
                            "type": 1,
                            "duration": "00:00:00",
                            "start_time": 1561029280,
                            "end_time": 1561029280,
                            "action": "received",
                            "received_by": [
                                {
                                    "user_id": "5d08992bd3b04894",
                                    "name": "Manras Technology",
                                    "email": "rohit.aryan@manras.in",
                                    "extension": "10",
                                    "contact_number_raw": "+919041910069",
                                    "contact_number": "9041910069"
                                }
                            ]
                        }
                    ],
                    "_us": [
                        {
                            "ky": "5d08992bd3b04894",
                            "vl": "received"
                        }
                    ],
                    "_tc": [
                        {
                            "ye": "5d08992bd3b04894",
                            "yf": 0
                        }
                    ]
                }
            },
            {
                "_index": "latestlogs",
                "_type": "logs",
                "user_id": "6d3733ce3dcd084e1cf38e8a0f8c6efcbd2e975a",
                "_score": 16.142824,
                "_source": {
                    "allcaller_id": "6d3733ce3dcd084e1cf38e8a0f8c6efcbd2e975a",
                    "source": "2",
                    "company_id": "5d08992bd0309594",
                    "caller_number": "+915227188769",
                    "caller_number_raw": "5227188769",
                    "_cm": "",
                    "start_time": 1561028633,
                    "_ms": 1561028633000,
                    "seconds": 39833,
                    "end_time": 1561028633,
                    "synced_time": 1561034510,
                    "_drs": 0,
                    "duration": "00:00:00",
                    "_drm": 0,
                    "type": 1,
                    "event": 1,
                    "status": 1,
                    "fileurl": "",
                    "filename": "",
                    "comments": null,
                    "additional_parameters": [
                        {
                            "ky": "unique_id",
                            "vl": "5d0b7f0eb2a91576"
                        },
                        {
                            "ky": "is_starred",
                            "vl": "0"
                        },
                        {
                            "ky": "view_type",
                            "vl": "2"
                        },
                        {
                            "ky": "is_commented",
                            "vl": "0"
                        },
                        {
                            "ky": "is_archived",
                            "vl": "0"
                        }
                    ],
                    "country": "91",
                    "state": "UP, IN",
                    "log_details": [
                        {
                            "text": "",
                            "type": 1,
                            "duration": "00:00:00",
                            "start_time": 1561028633,
                            "end_time": 1561028633,
                            "action": "received",
                            "received_by": [
                                {
                                    "user_id": "5d08992bd3b04894",
                                    "name": "Manras Technology",
                                    "email": "rohit.aryan@manras.in",
                                    "extension": "10",
                                    "contact_number_raw": "+919041910069",
                                    "contact_number": "9041910069"
                                }
                            ]
                        }
                    ],
                    "_us": [
                        {
                            "ky": "5d08992bd3b04894",
                            "vl": "received"
                        }
                    ],
                    "_tc": [
                        {
                            "ye": "5d08992bd3b04894",
                            "yf": 0
                        }
                    ]
                }
            }

 
Ajay K DubediAjay K Dubedi
Hi golu,

Used the below code :

 
String s ='{"status": "success", "code": 200, "data": {  "total": 71, "max_score": null, "hits": [ { "_index": "latestlogs", "_type": "logs", "user_id": "79b4935e0bc4e689bf2fe60ba2fed8bc7b914e4e", "_score": 16.230835, "_source": { "allcaller_id": "79b4935e0bc4e689bf2fe60ba2fed8bc7b914e4e", "source": "2", "company_id": "5d08992bd0309594", "caller_number": "+916289030819", "caller_number_raw": "6289030819", "_cm": "","start_time": 1561034490, "_ms": 1561034490000, "seconds": 45690,  "end_time": 1561034492,  "synced_time": 1561034510,  "_drs": 2,  "duration": "00:00:02", "_drm": 0.03, "type": 1,  "event": 2, "status": 1, "fileurl": "", "filename": "","comments": null, "additional_parameters": [ {"ky": "unique_id", "vl": "5d0b7f0ed0eca615" },{ "ky": "is_starred",  "vl": "0"  }, {  "ky": "view_type",  "vl": "2"  },  { "ky": "is_commented", "vl": "0" }, { "ky": "is_archived", "vl": "0" } ],"country": "91","state": "IN","log_details": [{"text": "","type": 1,"duration": "00:00:02","start_time": 1561034490,"end_time": 1561034492,"action": "received","received_by": [{"user_id": "5d08992bd3b04894","name": "Manras Technology","email": "rohit.aryan@manras.in","extension": "10", "contact_number_raw": "+919041910069","contact_number": "9041910069" }]}],"_us": [{"ky": "5d08992bd3b04894","vl": "received"}],"_tc": [{"ye": "5d08992bd3b04894","yf": 2}]}},{"_index": "latestlogs","_type": "logs","user_id": "646a535ebd481cb362c71ca1e8922ad698639d01","_score": 16.142824,"_source": {"allcaller_id": "646a535ebd481cb362c71ca1e8922ad698639d01","source": "2","company_id": "5d08992bd0309594", "caller_number": "+915227188769","caller_number_raw": "5227188769","_cm": "","start_time": 1561029280,"_ms": 1561029280000,"seconds": 40480,"end_time": 1561029280,"synced_time": 1561034507,"_drs": 0,"duration": "00:00:00","_drm": 0,"type": 1,"event": 1,"status": 1, "fileurl": "","filename": "","comments": null,"additional_parameters": [{"ky": "unique_id","vl": "5d0b7f0be1da7596"},{"ky": "is_starred","vl": "0"},{"ky": "view_type","vl": "2"},{"ky": "is_commented","vl": "0"},{"ky": "is_archived","vl": "0"}],"country": "91","state": "UP, IN","log_details": [{"text": "", "type": 1,"duration": "00:00:00","start_time": 1561029280,"end_time": 1561029280,"action": "received","received_by": [{ "user_id": "5d08992bd3b04894", "name": "Manras Technology","email": "rohit.aryan@manras.in","extension": "10","contact_number_raw": "+919041910069","contact_number": "9041910069"}]}],"_us": [{"ky": "5d08992bd3b04894","vl": "received"}],"_tc": [{"ye": "5d08992bd3b04894","yf": 0}]}},{ "_index": "latestlogs","_type": "logs","user_id": "6d3733ce3dcd084e1cf38e8a0f8c6efcbd2e975a","_score": 16.142824,"_source": { "allcaller_id": "6d3733ce3dcd084e1cf38e8a0f8c6efcbd2e975a", "source": "2","company_id": "5d08992bd0309594","caller_number": "+915227188769","caller_number_raw": "5227188769","_cm": "","start_time": 1561028633,"_ms": 1561028633000,"seconds": 39833,"end_time": 1561028633,"synced_time": 1561034510,"_drs": 0, "duration": "00:00:00", "_drm": 0,"type": 1,"event": 1,"status": 1,"fileurl": "","filename": "","comments": null,"additional_parameters": [{"ky": "unique_id","vl": "5d0b7f0eb2a91576"},{"ky": "is_starred","vl": "0"},{"ky": "view_type","vl": "2"},{"ky": "is_commented","vl": "0"},{"ky": "is_archived","vl": "0"}],"country": "91","state": "UP, IN","log_details": [{"text": "","type": 1,"duration": "00:00:00","start_time": 1561028633,"end_time": 1561028633,"action": "received","received_by": [{"user_id": "5d08992bd3b04894","name": "Manras Technology","email": "rohit.aryan@manras.in", "extension": "10",  "contact_number_raw": "+919041910069", "contact_number": "9041910069" }] } ],"_us": [ {"ky": "5d08992bd3b04894","vl": "received"}],"_tc": [{"ye": "5d08992bd3b04894","yf": 0 }]}}]}}';    String s ='{"status": "success", "code": 200, "data": {  "total": 71, "max_score": null, "hits": [ { "_index": "latestlogs", "_type": "logs", "user_id": "79b4935e0bc4e689bf2fe60ba2fed8bc7b914e4e", "_score": 16.230835, "_source": { "allcaller_id": "79b4935e0bc4e689bf2fe60ba2fed8bc7b914e4e", "source": "2", "company_id": "5d08992bd0309594", "caller_number": "+916289030819", "caller_number_raw": "6289030819", "_cm": "","start_time": 1561034490, "_ms": 1561034490000, "seconds": 45690,  "end_time": 1561034492,  "synced_time": 1561034510,  "_drs": 2,  "duration": "00:00:02", "_drm": 0.03, "type": 1,  "event": 2, "status": 1, "fileurl": "", "filename": "","comments": null, "additional_parameters": [ {"ky": "unique_id", "vl": "5d0b7f0ed0eca615" },{ "ky": "is_starred",  "vl": "0"  }, {  "ky": "view_type",  "vl": "2"  },  { "ky": "is_commented", "vl": "0" }, { "ky": "is_archived", "vl": "0" } ],"country": "91","state": "IN","log_details": [{"text": "","type": 1,"duration": "00:00:02","start_time": 1561034490,"end_time": 1561034492,"action": "received","received_by": [{"user_id": "5d08992bd3b04894","name": "Manras Technology","email": "rohit.aryan@manras.in","extension": "10", "contact_number_raw": "+919041910069","contact_number": "9041910069" }]}],"_us": [{"ky": "5d08992bd3b04894","vl": "received"}],"_tc": [{"ye": "5d08992bd3b04894","yf": 2}]}},{"_index": "latestlogs","_type": "logs","user_id": "646a535ebd481cb362c71ca1e8922ad698639d01","_score": 16.142824,"_source": {"allcaller_id": "646a535ebd481cb362c71ca1e8922ad698639d01","source": "2","company_id": "5d08992bd0309594", "caller_number": "+915227188769","caller_number_raw": "5227188769","_cm": "","start_time": 1561029280,"_ms": 1561029280000,"seconds": 40480,"end_time": 1561029280,"synced_time": 1561034507,"_drs": 0,"duration": "00:00:00","_drm": 0,"type": 1,"event": 1,"status": 1, "fileurl": "","filename": "","comments": null,"additional_parameters": [{"ky": "unique_id","vl": "5d0b7f0be1da7596"},{"ky": "is_starred","vl": "0"},{"ky": "view_type","vl": "2"},{"ky": "is_commented","vl": "0"},{"ky": "is_archived","vl": "0"}],"country": "91","state": "UP, IN","log_details": [{"text": "", "type": 1,"duration": "00:00:00","start_time": 1561029280,"end_time": 1561029280,"action": "received","received_by": [{ "user_id": "5d08992bd3b04894", "name": "Manras Technology","email": "rohit.aryan@manras.in","extension": "10","contact_number_raw": "+919041910069","contact_number": "9041910069"}]}],"_us": [{"ky": "5d08992bd3b04894","vl": "received"}],"_tc": [{"ye": "5d08992bd3b04894","yf": 0}]}},{ "_index": "latestlogs","_type": "logs","user_id": "6d3733ce3dcd084e1cf38e8a0f8c6efcbd2e975a","_score": 16.142824,"_source": { "allcaller_id": "6d3733ce3dcd084e1cf38e8a0f8c6efcbd2e975a", "source": "2","company_id": "5d08992bd0309594","caller_number": "+915227188769","caller_number_raw": "5227188769","_cm": "","start_time": 1561028633,"_ms": 1561028633000,"seconds": 39833,"end_time": 1561028633,"synced_time": 1561034510,"_drs": 0, "duration": "00:00:00", "_drm": 0,"type": 1,"event": 1,"status": 1,"fileurl": "","filename": "","comments": null,"additional_parameters": [{"ky": "unique_id","vl": "5d0b7f0eb2a91576"},{"ky": "is_starred","vl": "0"},{"ky": "view_type","vl": "2"},{"ky": "is_commented","vl": "0"},{"ky": "is_archived","vl": "0"}],"country": "91","state": "UP, IN","log_details": [{"text": "","type": 1,"duration": "00:00:00","start_time": 1561028633,"end_time": 1561028633,"action": "received","received_by": [{"user_id": "5d08992bd3b04894","name": "Manras Technology","email": "rohit.aryan@manras.in", "extension": "10",  "contact_number_raw": "+919041910069", "contact_number": "9041910069" }] } ],"_us": [ {"ky": "5d08992bd3b04894","vl": "received"}],"_tc": [{"ye": "5d08992bd3b04894","yf": 0 }]}}]}}';    Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);    System.debug('status  '+ m.get('status'));    System.debug('data  '+m.get('data'));

For more details go to below link:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi