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
Robin Barnwell 3Robin Barnwell 3 

JSON Class Assignment

I'm integrating Salesforce with Mailchimp as the default connecting isn't doing what I need.  I've created a simple class to hold the REST Body

public class MailChimpSubscriberJson{
    public class Merge_fields {
        public String FNAME;
        public String LNAME;
    }
    public class JSON2Apex {
        public String id;
        public String email_address;
        public String status;
        public Merge_fields merge_fields;
    }
}

But my assignment works for apart of the sub-class, Merge_fields.  Specifically this line of code is wrong, but I can't see why.  There is data in the Firstname.

                Subscriber.merge_fields.FNAME=MailchimpContact[0].Firstname;

I'm getting this error, common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object.  I can only think Subscriber.merge_fields.FNAME is incorrect.
bk sbk s
From the error it is somewhat clear that JSON2Apex class when used for deserializing is not mapping up merge_fields member variable . With that being said when you try to do Subscriber.merge_fields.FNAME = xxxxx , it will for sure throw you an error . For better understanding could you please post the JSON?
Robin Barnwell 3Robin Barnwell 3
I think I've solved it.  I used the recommended utility, https://json2apex.herokuapp.com/ to generate the APEX class.  After some experimintation with the output we added this extra bit to make it work:

public class MailChimpSubscriberJson{
    public class Merge_fields {
        public String FNAME;
        public String LNAME;
    }
    public class JSON2Apex {
        public String id;
        public String email_address;
        public String status;
        public Merge_fields merge_fields = new Merge_fields();
    }    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    } 
}

I'll make contact with the authors to see what they think.  Either way we've solved the problem.