For starters, I don't know if I'm asking this correctly so I request people refrain from providing me with negative points. The issue I'm having is that I'm unable to specify two JSON calls as my Map input variables and I'm trying to see if the reason is a limitation of Apex itself or something else I might not be doing right. I could forgo the option of storing them in separate String variables but I would like to check and confirm there is no other way to do this. It seems storing directly into variables would be a waste of time if this for loop processes 1000x in my Apex script.

Code snippet:

 for(Integer i=0;i<jsonClass.value.size();i++) {
        if(jsonClass.value[i].folder!=null) {
        Map<String, String> ret_var = new Map<String, String>();
        ret_var.put(jsonClass.value[i].name, jsonClass.value[i].folder);

JSON class:

public virtual class JSON2Apex {

    public List<Value> value {get;set;} 

    public class Value {
        public String name {get;set;} 
        public folder folder {get;set;} 
        public file file {get;set;} 

    public class folder {
        public String id {get;set;} 
        public Integer childCount {get;set;} 

    public class file {
        public String mimeType {get;set;} 

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

Error message:

Method does not exist or incorrect signature: void put(String, JSON2Apex.folder) from the type Map

  • 1
    Can we see some example json? Do you have a class definition for your jsonClass? Is there an error message provided? These could included via edit – battery.cord Aug 27 '18 at 13:40
  • It isn't really clear what your exact problem is. Are you getting an error message (you should include it verbatim if you are getting one)? Is your ret_var not holding the values you expect (could be an issue of the scope of your map)? – Derek F Aug 27 '18 at 13:45
  • Are you looking for a function like map() or reduce() as found in another programming language? Apex doesn't have those natively, and functions aren't first-class values in Apex, so iteration is typically the idiom you use. – David Reed Aug 27 '18 at 13:45
  • 1
    What you have right now should not produce any errors (assuming folder and name are member variables of your jsonClass). The way most of the languages that I've worked with deal with method calls/expressions as parameters to another method is to first evaluate the expression, and then pass the evaluated value to the method being called. Apex exhibits this behavior as well. – Derek F Aug 27 '18 at 13:49
  • @battery.cord I posted my JSON class. Now, can you please tell me why you need to see example JSON as well? This does not make sense because I'm trying to pass two functions that work in system.debug as inputs to my Map variable 'ret_var' – murkywaters Aug 27 '18 at 13:51

You have a type mismatch between your map and your json class. You're trying to pass a value to a String, but the value isn't a String, its a folder. This is where seeing the error message & underlying class is important.

Looks like all you need to do to access the value of your folder is to change your put to this:

ret_var.put(jsonClass.value[i].name, jsonClass.value[i].folder.Id);

Doing so accesses the folder classes variable and fits your map's String typing.

  • That's what I was realizing just now. I'll try the ".Id" but in the future, would there be any forseeable way to directly add the single quotes in the functions like through the use of regex? – murkywaters Aug 27 '18 at 14:01
  • 2
    Those aren't functions - you've created a class with other, internal class, which are represented as their own objects, with their own fields. You'll need to access their internal variables as needed to get primitive types for use in maps. The structure is there in the class you're using to hold your json data, just need to have your code match its structure. – battery.cord Aug 27 '18 at 14:07
  • True. On the safe side, I added some regex to introduce single quotes into the Map variable I was referencing since their type is String. – murkywaters Aug 27 '18 at 16:04

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.