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
Sunita PSunita P 

json.deserialize is returning null for date

Hello everyone,

I'm working with the geonames web services, the datetime field is being returned as null when I'm deserializing as follows--

 ResCls eqList = (ResCls)JSON.deserialize(responseBody,ResCls.class);

Here's the wrapper class-
public class ResCls{
        List <Earthquakes> earthQuakes;
        
        public ResCls(List <Earthquakes> eqlist1){
            earthQuakes= eqlist1;
        }
    }
    
    public class Earthquakes{
        public Datetime eqDatetime;
        public Double depth;
        public Double lng;
        public String src;
        public String eqId;
        public Double lat;
        public Double magnitude;
        
        public Earthquakes(Datetime eqd, Double dep, Double lng1, string src1, string eqid2, Double mag, Double lat1){
            eqDatetime = eqd;
            depth = dep;
            lng = lng1;
            src = src1;
            eqId=eqid2;
            magnitude = mag;
            lat = lat1;
        
        }
           
    }

Here's the sample JSON that is being deserialized-
{"earthquakes":[{"datetime":"2011-03-11 04:46:23","depth":24.4,"lng":142.369,"src":"us","eqid":"c0001xgp","magnitude":8.8,"lat":38.322},{"datetime":"2012-04-11 06:38:37","depth":22.9,"lng":93.0632,"src":"us","eqid":"c000905e","magnitude":8.6,"lat":2.311},{"datetime":"2007-09-12 09:10:26","depth":30,"lng":101.3815,"src":"us","eqid":"2007hear","magnitude":8.4,"lat":-4.5172},{"datetime":"2012-04-11 08:43:09","depth":16.4,"lng":92.4522,"src":"us","eqid":"c00090da","magnitude":8.2,"lat":0.7731},{"datetime":"2007-04-01 18:39:56","depth":10,"lng":156.9567,"src":"us","eqid":"2007aqbk","magnitude":8,"lat":-8.4528},{"datetime":"2015-04-25 06:13:40","depth":15,"lng":84.6493,"src":"us","eqid":"us20002926","magnitude":7.9,"lat":28.1306},{"datetime":"2016-12-17 11:00:30","depth":103.19,"lng":153.4495,"src":"us","eqid":"us200081v8","magnitude":7.9,"lat":-4.5091},{"datetime":"2007-09-12 21:49:01","depth":10,"lng":100.9638,"src":"us","eqid":"2007hec6","magnitude":7.8,"lat":-2.5265},{"datetime":"2016-03-02 12:55:00","depth":24,"lng":94.275,"src":"us","eqid":"us10004u1y","magnitude":7.8,"lat":-4.9082},{"datetime":"2015-05-30 11:36:00","depth":677.56,"lng":140.4932,"src":"us","eqid":"us20002ki3","magnitude":7.8,"lat":27.8312}]}

I have tried using 'string', that is also returning datetime as null, all other fields are being deserialezed just fine. Not sure what could be the issue.

Thnak you.
 
Akhil AnilAkhil Anil
Hi Sunita,

You are getting it as null because the variable name in your wrapper class is eqDatetime where as the variable name in the JSON is simply datetime which is a mismatch. The variable names in the JSON and the wrapper should exactly match. The problem here is that you cannot define the variable in your wrapper as "datetime" as it's a reserved keyword in apex. An alternative solution is to play around as I have shown in the below code.

Modify your parsing statement like this which will ensure that the JSON variable matches with your wrapper class variable
 
responseBody.replace('"datetime":','"eqDatetime":');

ResCls eqList = (ResCls)JSON.deserialize(responseBody,ResCls.class);

Then ONLY change your Earthquates class so that eqDatetime is treated as a String rather than Datetime because the value is thrown to us as a quoted string in the JSON.
 
public class Earthquakes{
        public String eqDatetime;
        public Double depth;
        public Double lng;
        public String src;
        public String eqId;
        public Double lat;
        public Double magnitude;
        
        public Earthquakes(String eqd, Double dep, Double lng1, string src1, string eqid2, Double mag, Double lat1)    {
            eqDatetime = eqd;
            depth = dep;
            lng = lng1;
            src = src1;
            eqId=eqid2;
            magnitude = mag;
            lat = lat1;
        
        }
           
    }
That should do the trick !

Kindly mark it as an answer if that works !
Sunita PSunita P
Hello Akhil,

I did try parsing the datetime as a String, that did not solve issue, the datetime was being returned as NULL.

However, I used the JSONParser class and parsed one token at a time, that worked and I was able to retrieve the the datetime value.

Thank you.
Labhansh GuptaLabhansh Gupta
Thank You Akhil for the answer . Chnaging the data type from DateTime to string helped me in this case. Also I would like to add one more point . Sunita  , Just make a new string for the json string with replaced texts (New responseBody string with the replaced texts). And then pass this new string as an input to the deserialize function. 
 
// responseBody = ' This stores the complete Json string . Remove the List name "earthquakes" from the Input Json string . '

responseBody = '[{"datetime":"2011-03-11 04:46:23","depth":24.4,"lng":142.369,"src":"us","eqid":"c0001xgp","magnitude":8.8,"lat":38.322},{"datetime":"2012-04-11 06:38:37","depth":22.9,"lng":93.0632,"src":"us","eqid":"c000905e","magnitude":8.6,"lat":2.311},{"datetime":"2007-09-12 09:10:26","depth":30,"lng":101.3815,"src":"us","eqid":"2007hear","magnitude":8.4,"lat":-4.5172},{"datetime":"2012-04-11 08:43:09","depth":16.4,"lng":92.4522,"src":"us","eqid":"c00090da","magnitude":8.2,"lat":0.7731},{"datetime":"2007-04-01 18:39:56","depth":10,"lng":156.9567,"src":"us","eqid":"2007aqbk","magnitude":8,"lat":-8.4528},{"datetime":"2015-04-25 06:13:40","depth":15,"lng":84.6493,"src":"us","eqid":"us20002926","magnitude":7.9,"lat":28.1306},{"datetime":"2016-12-17 11:00:30","depth":103.19,"lng":153.4495,"src":"us","eqid":"us200081v8","magnitude":7.9,"lat":-4.5091},{"datetime":"2007-09-12 21:49:01","depth":10,"lng":100.9638,"src":"us","eqid":"2007hec6","magnitude":7.8,"lat":-2.5265},{"datetime":"2016-03-02 12:55:00","depth":24,"lng":94.275,"src":"us","eqid":"us10004u1y","magnitude":7.8,"lat":-4.9082},{"datetime":"2015-05-30 11:36:00","depth":677.56,"lng":140.4932,"src":"us","eqid":"us20002ki3","magnitude":7.8,"lat":27.8312}]' ;

string newResponseBody = responseBody.replace('"datetime":', '"eqDatetime":');
list<earthquakes> eQuakeJsonList = (list<earthquakes>)JSON.deserialize(newResponseBody, list<earthquakes>.class);