I'm using a bad practice in my code. I get all needed values from JSON like this:

    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('Endpoint is here');
    HttpResponse response = http.send(request);

    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    List<Object> received = (List<Object>) results.get('tools');
    List<Obj__c> newCardsList = new List<Obj__c>();

    for(Object each : received){
        String inText = String.valueOf(each);
        String name = inText.substringBetween('), name=', '}');
        String cardId = inText.substringBetween('id=', ',');
        //The biggest trouble:
        String dueDateInText = inText.substringBetween('due=', 'T');
        Date dueDate;
        if(dueDateInText != null){
            dueDate = Date.valueOf(dueDateInText);

How do i do it right? Especially with the date: the required value is between due= and T.

enter image description here

2 Answers 2


When you use deserializeUntyped(), you get an Object back, which is usually either cast to Map<String, Object> or List<Object>.

The thing that you're missing here is that each object that you get from your Map or List can also be cast.

Applying this to your loop, we get

for(Object each : received){
    Map<String, Object> nextLevel = (Map<String,Object>)each;

    String name = (String)nextLevel.get('name');
    Date dueDate = (Date)nextLevel.get('due');

Of course, if you don't want to bother with casting everything all the time, you could create an Apex class that mimics your JSON structure and use JSON.deserialize(jsonStr, MyDeserializationClass.class);

  • Thank you so much! And what if we have {labels=({due=null, name=}), name=Name}? I can't get due like that. Jul 11, 2019 at 13:58
  • 1
    @MichaelLev19 Yes you can. Just apply the same pattern until you get to your desired level. Like I said in my answer, each object you get from your Map or List can also be cast. Expanding on that a bit, each JSON object can be cast to a Map<String, Object>, each JSON list can be cast to a List<Object>, and individual values (name, due, id, etc...) can be cast to more concrete types (String, Date, etc...). I'd encourage you to play around with this yourself for a while via anonymous apex. You'll likely find it easier to internalize this if you figure out the exact code to use yourself.
    – Derek F
    Jul 11, 2019 at 14:40
  • 1
    @MichaelLev19 I also hope that it's becoming clear why making a class to do typed deserialization is an attractive option. Sure, you can use loops and type-cast everything, but making a class to deserialize this saves you the trouble. You'd be able to access your data using more familiar means, something like Date dueDate = deserialized.tools[0].labels.due; for example (though encountering a collection is a pretty strong indicator that you'll want to loop over it instead of accessing a single index).
    – Derek F
    Jul 11, 2019 at 14:45

if you would like to get the date/datetim, you can try the following code:

String s = '2019-07-09T09:27:29Z';
s = s.replace('T', ' ').replace('Z', '');
Datetime theDatetime = Datetime.valueOf(s);
// or if you just want to get a date format
Date theDate = theDatetime.date();
  • No, I need only date :) Can I take the values from JSON another way? I really think it's a little silly of me to do this through substring. Jul 11, 2019 at 11:45

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.