0

I am unable to wrap the Array of JSON object in apex using wrapper

JSON

    {
    "personInfo": {
        "firstName": "Sudhanshu",
        "lastName": "Singhs"
    },
    "contactInfo": [
        {
            "mobile": "1231",
            "email": "[email protected]"
        },
        {
            "mobile": "1231",
            "email": "[email protected]"
        }
    ]
}

Wrapper Class

public class SampleWrapper {
    public PersonInformation personInfo;
    public List<ContactInformation> contactInfo;
        
    public class ContactInformation{
        public String mobile;
        public String email;
    }
    public class PersonInformation{
        public String firstName;
        public String lastName;
    }
    
}

My Class

public class SampleWrapperValidate {
    public static void validate(){
        Conversion_Service_Configuration__mdt   con = Conversion_Service_Configuration__mdt.getInstance('sampleConfig');
        String json = con.JSON_Configuration__c;
        
        SampleWrapper wrp =  (SampleWrapper) System.JSON.deserialize(json, SampleWrapper.class);
        System.debug(wrp.contactInfo);
        
    }
}

ERROR: System.JSONException: Unexpected end-of-input within/between OBJECT entries at [line:13, column:267]

enter image description here

1 Answer 1

0

I have tried your code in my org. just skip the custom metadata part and it works fine. Make sure that the JSON coming from the custom metadata is valid.

Wrapper Class:

public class SampleWrapper {
    public PersonInformation personInfo;
    public List<ContactInformation> contactInfo;
    
    public class ContactInformation{
        public String mobile;
        public String email;
    }
    public class PersonInformation{
        public String firstName;
        public String lastName;
    }    
}

Apex Class:

public class SampleWrapperValidate {
    public static void validate(){
        //Conversion_Service_Configuration__mdt   con = Conversion_Service_Configuration__mdt.getInstance('sampleConfig');
        //String json = con.JSON_Configuration__c;
        
        String jsonData = '{"personInfo":{"firstName":"Sudhanshu","lastName":"Singhs"},"contactInfo":[{"mobile":"1231","email":"[email protected]"},{"mobile":"1231","email":"[email protected]"}]}';
        
        SampleWrapper wrp =  (SampleWrapper) System.JSON.deserialize(jsonData, SampleWrapper.class);
        System.debug(wrp.contactInfo);
        
    }
}

Click on Debug in the developer console, click on the Open Execute Anonymous Window and run the below code.

SampleWrapperValidate.validate();

Output::

enter image description here

You must log in to answer this question.

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