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
Steve BerleySteve Berley 

deserializing json datetime yields null

I'm trying to deserialize a date/time from a twitter feed into Salesforce and am running into a problem where it always yields a null.

json:
{
  "data": [
    {
      "created_at": "2021-06-12T00:00:01.000Z",
      "id": "1403502310429392901",
      "text": "She means business \uD83D\uDC69\uD83C\uDFFD‍⚖️ Gugu Mbatha-Raw is Judge Renslayer in Marvel Studios' #Loki. Stream new episodes of the Original Series every Wednesday on @DisneyPlus. https://t.co/IwpY2eUvwd"
    } ]
}

Deserializer from SuperFell's tool
// Generated by JSON2Apex http://json2apex.herokuapp.com/
public class Twitter_TimelineParser {
    public class Data {
        public String created_at;
        public String id;
        public String text;
    }
    
    public static Twitter_TimelineParser parse(String json) {
        return (Twitter_TimelineParser) System.JSON.deserialize(json, Twitter_TimelineParser.class);
    }
}

created_at always returns a null, whether I try to capture it as a DateTime, Date or String. I really don't which type I get as long as I get the data since I can always manipulate it later.

Any idea's why?

Obviously this is the compact version relying on the native parser. I've also tried with the involved one and tried to capture the string then convert it within the parser. Still only yields null.

Thanks,
Maharajan CMaharajan C
Hi Steve,

The wrapper class genearated is wrong. It should be in below format. Data needs to be wrapped in Array so the below line is missing in your wrapper.

public List<Data> data;
 
public class Twitter_TimelineParser {

	public List<Data> data;

	public class Data {
		public DateTime created_at;  // You can use DateTime here it's not a problem
		public String id;
		public String text;
	}

	
	public static Twitter_TimelineParser parse(String json) {
		return (Twitter_TimelineParser) System.JSON.deserialize(json, Twitter_TimelineParser.class);
	}
}

In Apex Class you have to use the below code for deserialization.
 
Twitter_TimelineParser resp = Twitter_TimelineParser.parse(JsonStr);

system.debug( ' Data ==> ' +  resp.Data );

system.debug( ' id ==> ' +  resp.Data[0].id );

system.debug( ' created_at ==> ' +  resp.Data[0].created_at );


Thanks,
Maharajan.C