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
sharan kumar Kollurusharan kumar Kolluru 

How to Deserialize JSON Response and Store the Response in Custom Object.

Hi Friends,

I Have a Requirement, I Need to Deserialize the JSON Response and Store in to custom Object.

My JSON Response Format: 
{
    "_links": {
        "_self": "/proxy",
        "_parent": "/"
    },
    "ip": "92.252.241.11",
    "port": 4145,
    "protocol": "socks4",
    "anonymity": "high anonymity",
    "lastTested": "2020-06-19 09:55:54",
    "allowsRefererHeader": true,
    "allowsUserAgentHeader": true,
    "allowsCustomHeaders": true,
    "allowsCookies": true,
    "allowsPost": true,
    "allowsHttps": true,
    "country": "RU",
    "connectTime": "3.312",
    "downloadSpeed": "50.000",
    "secondsToFirstByte": "3.472",
    "uptime": "64.125"
}

To Achieve this Requirement, I Have created a custom Object as Proxy_Server__c and Some Custom Fields to store the Response.

But I'm getting an Error as Compile Error: Incompatible types since an instance of Map<String,Object> is never an instance of List<Object> at line 19 column 34

public class GetProxyDataCallout {
    public static HttpResponse makeGetProxyDataCallout(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.getproxylist.com/proxy');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        System.debug(response);
        System.debug(response.getBody());
        
        List<Proxy_Server__c> upsertGetProxyList = new List<Proxy_Server__c>();
     
        //Deserialize The JSON Response in to List of Objects
        Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
        System.debug(results);
        
        List<Object> proxyList = (List<Object>)results;
        System.debug(proxyList);
        
        for(Object proxyListobj : proxyList){
            System.debug(proxyListobj);
            Map<string, Object> proxyAddress = (Map<string, Object>)proxyListObj;
            System.debug(proxyAddress.get('ip'));
            Proxy_Server__c proxyIpList = new Proxy_Server__c();
            proxyIpList.Name = (String)proxyAddress.get('country');
            proxyIpList.IP_Address__c = (String)proxyAddress.get('ip');
            proxyIpList.Port__c = (String)proxyAddress.get('port');
            proxyIpList.Last_Update__c = (String)proxyAddress.get('lastTested');
            proxyIpList.Proxy_Level__c = (String)proxyAddress.get('anonymity');
            proxyIpList.Provider__c = 'Get Proxy';
            proxyIpList.Proxy_Provider__c = string.isNotBlank(proxyServerId)?proxyServerId:'';
            
            upsertGetProxyList.add(proxyIpList);  
        }
        
        UPSERT upsertGetProxyList;
        return response;
    }

}


Please Help me to resolve the issue.
Thanks In Advance.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sharan,

You could use something like https://json2apex.github.io/ to create a blueprint to deserialize the json response you get.

I hope this helps.

Regards,
Anutej
David Zhu 🔥David Zhu 🔥
You may follow the code snippet below.

1. Define the following class based on HTTP response to this class:
public with sharing class JSONClass {

        public String ip {get;set;}
        public Integer port {get;set;}
        public String protocol {get;set;}
        public String anonymity {get;set;}
        public String lastTested {get;set;}
        public Boolean allowsRefererHeader {get;set;}
        public Boolean allowsUserAgentHeader {get;set;}
        public Boolean allowsCustomHeaders {get;set;}
        public Boolean allowsCookies {get;set;}
        public Boolean allowsPost {get;set;}
        public Boolean allowsHttps {get;set;}
        public String country {get;set;}
        public String connectTime {get;set;}
        public String downloadSpeed {get;set;}
        public String secondsToFirstByte {get;set;}
        public String uptime {get;set;}
}

2. Use the following line to deserialize http reponse body.
JSONClass myJSONInstance = (JSONClass)JSON.deserialize(response.getBody(), JSONClass.class);

3. Build Proxy_Server__c object data and insert. the reponse does not return map or list at all, not sure why you tried to deserialize http reponse body to map and list.

            Proxy_Server__c proxyIpList = new Proxy_Server__c();
            proxyIpList.Name = myJSONInstance .country';
            proxyIpList.IP_Address__c = myJSONInstance.ip;
            proxyIpList.Port__c = myJSONInstance.port;
            proxyIpList.Last_Update__c = myJSONInstance.lastTested;
            proxyIpList.Proxy_Level__c = myJSONInstance.anonymity;
            proxyIpList.Provider__c = 'Get Proxy';
            proxyIpList.Proxy_Provider__c = myJSONInstance.proxyServerId;  

            insert proxyIpList;