3

I think I am just missing something simple - using very simplified and generic code for example.

I have some JSON:

{
   "level1":[
      {
         "level2":{
            "id":119,
            "createdAt":"2024-05-06T17:36:32.533Z",
            "user":{
               "id":1,
               "firstName":"First",
               "lastName":"Name",
               "email":"[email protected]",
               "emailUnsubscribed":"false"
            },
            "organization":"Acme",
            "details":[
               {
                  "d1":{
                     "ref":"ref1",
                     "name":"BetterName",
                     "moreDetails":[
                        {
                           "detail1":{
                              "name":"smith",
                              "random":"123123123"
                           }
                        }
                     ]
                  }
               },
               {
                  "d1":{
                     "ref":"reg2",
                     "name":"Name2",
                     "moreDetails":[
                        {
                           "detail1":{
                              "name":"ccg",
                              "random":"333444555"
                           }
                        },
                        {
                           "detail1":{
                              "name":"acg",
                              "random":"555666777"
                           }
                        }
                     ]
                  }
               }
            ]
         }
      }
   ]
}

That I use a Wrapper class to parse:

public class fromJSON{
    public cls_level1[] level1;
    class cls_level1 {
        public cls_level2 level2;
    }
    class cls_level2 {
        public Integer id;  
        public String createdAt;    
        public cls_user user;
        public String organization; 
        public cls_details[] details;
    }
    class cls_user {
        public Integer id;  
        public String firstName;    
        public String lastName; 
        public String email;    
        public String emailUnsubscribed;    
    }
    class cls_details {
        public cls_d1 d1;
    }
    class cls_d1 {
        public String ref;  
        public String name; 
        public cls_moreDetails[] moreDetails;
    }
    class cls_moreDetails {
        public cls_detail1 detail1;
    }
    class cls_detail1 {
        public String name; 
        public String random;   
    }
    public static fromJSON parse(String json){
        return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
    }

}

But when I do something like:

fromJSON testWrapper = fromJSON.parse(json);
System.debug(testWrapper.level1);
|DEBUG|(cls_level1:[level2=cls_level2:[createdAt=2024-05-06T17:36:32.533Z, details=(cls_details:[d1=cls_d1:[moreDetails=(cls_moreDetails:[detail1=cls_detail1:[name=smith, random=123123123]]), name=BetterName, ref=ref1]], cls_details:[d1=cls_d1:[moreDetails=(cls_moreDetails:[detail1=cls


I can see the data, but cannot access it.

System.debug(testWrapper.level1.level2)
Variable does not exist: level2

Not sure what I am missing or forgetting.

Thanks in Advance!

1 Answer 1

3

testwrapper.level1 is a list, rather than a single instance. The List type itself doesn't have a level2 variable (as the error states).

So you need to iterate over the list to get at a single instance, from which you would be able to access level2.

// <OuterClass.InnerClass> needs to be used to reference inner classes
//   outside of the class that contains it (the inner class)
for(fromJSON.cls_level1 level1Instance : testWrapper.level1) {
    system.debug(level1Instance.level2);
}
4
  • Ugh, I new I was missing something simple. THANK YOU!
    – Dar Wright
    Commented May 13 at 17:59
  • And if I wanted to get to say the details in level2? Nested For loops do not seem like a good idea for this, maybe I need to change everything into Maps?
    – Dar Wright
    Commented May 13 at 18:37
  • 1
    Nested loops are an inevitability when you have nested collections (sets, maps, or lists). Nested loops aren't inherently evil, it's mostly just stuff of the form if(loop1Var.fieldA == loop2Var.fieldA) that you need to watch out for.
    – Derek F
    Commented May 13 at 18:50
  • Thank you again!!
    – Dar Wright
    Commented May 14 at 12:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .