-3

I'm running into obscure issues while executing my Apex code that calls references from a separate JSON2Apex class and I can't figure out where I went wrong.

JSON2Apex class:

public virtual class JSON2Apex {

    public class Value{
            public Folder folder;
            public File file;
    }

    public class Folder{
            public Integer childCount;
    }

    public class File{
            public String mimeType;
            public Map<String, String> hashes;
    }

    public Value value;

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

Main class calling JSON2Apex:

public class ClassX extends JSON2Apex{

    public static void method1(String string){
     ...
     JSON2Apex jsonClass = JSON2Apex.parse(res.getBody());
     System.debug(jsonClass.value.folder);
     System.debug(jsonClass.value.folder.childCount);
     System.debug(jsonClass.value.file);
     System.debug(jsonClass.value.file.mimetype);
     System.debug(jsonClass.value.file.hashes);
    }
}

Error message:

Execute Anonymous Error

Line: 15, Column: 1 System.JSONException: Expected JSON2Apex.Value but found

[line:1, column:130]

The error message points to my top-level class definition that calls JSON2Apex, but I can't figure out which of my System.debug calls above is causing the issue. I followed this guide, but their method of calling myClass.size, etc... doesn't seem to work for me because I get:

"Variable Does Not Exist: Childcount"

if I just use System.debug(jsonClass.childCount) as an example.

  • 5
    Can you update your post with the sample JSON that you are parsing? – Jayant Das Aug 21 '18 at 16:09
  • Yea, check out the first code block in this post: salesforce.stackexchange.com/questions/228852/… – xorLogic Aug 21 '18 at 16:34
  • 3
    As already mentioned in the answer by gNerb, your problem is that the JSON you have is an array of value attributes, whereas your definition expects only one value attribute. Also its advisable to put the JSON you are referring from other post in this post directly for more clarity. And that your JSON itself is not valid as its missing the closing ]. – Jayant Das Aug 21 '18 at 17:06
  • Eh, typo maybe. – xorLogic Aug 21 '18 at 17:10
4

The first problem I see is that your JSON (taken from here) is expecting value to be a list, not a single object.

Change public Value value; to public List<Value> value;

Also try to change your debug to something like this:

public class ClassX extends JSON2Apex{
    public static void method1(String string){
     ...
     JSON2Apex jsonClass = JSON2Apex.parse(res.getBody());
     for (JSON2Apex.value v : jsonClass.value) {
         System.debug(v.folder);
         System.debug(v.folder.childCount);
         System.debug(v.file);
         System.debug(v.file.mimetype);
         System.debug(v.file.hashes);
     }
  }
}

Here is an answer I like to link back to where I tried my best to explain converting JSON to apex. Personally, I never use JSON2Apex. Not because it's bad but because it's limited. Also if you rely too much on it you'll always struggle with issues like this.

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.