0

I am not able to deserialize my json

JSON

{
"Services.ServiceContract_SAM_APIResponse": {
    "Services.ServiceContract_SAM_APIResult": {
        "Services.row": [
            {
                "Services.SR_NBR": "1-233391976021",
                "Services.SR_CREATED": "2018-04-26T13:44:25.000Z",
                "Services.SR_TYPE": "Corrective Repair",
                "Services.IB_SERIAL": "[]",
            }
        ]
    }
}}

My Wrapper Class

public class CCP_ServiceReportWrapper {

public CCP_ServiceReportWrapper(){}

public CCP_ServiceReportWrapper(JSONParser parser) {
    while (parser.nextToken() != System.JSONToken.END_OBJECT) {
        if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
            String text = parser.getText();
            if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                if (text == 'Services.ServiceContract_SAM_APIResponse') {
                    Services_ServiceContract_SAM_APIResponse = new Services_ServiceContract_SAM_APIResponse(parser);
                } else {
                    System.debug(LoggingLevel.WARN, 'CCP_ServiceReportWrapper consuming unrecognized property: '+text);
                    consumeObject(parser);
                }
            }
        }
    }
}

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

public Services_ServiceContract_SAM_APIResponse Services_ServiceContract_SAM_APIResponse {get;set;} // in json: Services.ServiceContract_SAM_APIResponse

public class Services_ServiceContract_SAM_APIResponse {
    public Services_ServiceContract_SAM_APIResult Services_ServiceContract_SAM_APIResult {get;set;} // in json: Services.ServiceContract_SAM_APIResult

    public Services_ServiceContract_SAM_APIResponse(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'Services.ServiceContract_SAM_APIResult') {
                        Services_ServiceContract_SAM_APIResult = new Services_ServiceContract_SAM_APIResult(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Services_ServiceContract_SAM_APIResponse consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Services_ServiceContract_SAM_APIResult {
    public List<Services_row> Services_row {get;set;} // in json: Services.row

    public Services_ServiceContract_SAM_APIResult(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'Services.row') {
                        Services_row = arrayOfServices_row(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Services_ServiceContract_SAM_APIResult consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Services_row {
    public String SR_NBR {get;set;} // in json: Services.SR_NBR
    public String SR_CREATED {get;set;} // in json: Services.SR_CREATED
    public String SR_TYPE {get;set;} // in json: Services.SR_TYPE
    public String IB_SERIAL {get;set;} // in json: Services.IB_SERIAL

    public Services_row(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'Services.SR_NBR') {
                        SR_NBR = parser.getText();
                    } else if (text == 'Services.SR_CREATED') {
                        SR_CREATED = parser.getText();
                    } else if (text == 'Services.SR_TYPE') {
                        SR_TYPE = parser.getText();
                    } else if (text == 'Services.IB_SERIAL') {
                        IB_SERIAL = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Services_row consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

private static List<Services_row> arrayOfServices_row(System.JSONParser p) {
    List<Services_row> res = new List<Services_row>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new Services_row(p));
    }
    return res;
}

public static void consumeObject(System.JSONParser parser) {
    Integer depth = 0;
    do {
        System.JSONToken curr = parser.getCurrentToken();
        if (curr == System.JSONToken.START_OBJECT || 
            curr == System.JSONToken.START_ARRAY) {
            depth++;
        } else if (curr == System.JSONToken.END_OBJECT ||
            curr == System.JSONToken.END_ARRAY) {
            depth--;
        }
    } while (depth > 0 && parser.nextToken() != null);
}}

This is how I am deserializing

CCP_ServiceReportWrapper ObjServiceReportWrapper = new CCP_ServiceReportWrapper();
                ObjServiceReportWrapper = (CCP_ServiceReportWrapper) JSON.deserialize(res.getBody(), CCP_ServiceReportWrapper.class);
                system.debug('ObjServiceReportWrapper++++ '+ObjServiceReportWrapper);

res.getBody() has the valid response but in debug I am getting as Null

Any Idea for this? Thanks

  • 2
    would be a lot easier if you replace the namespaces 'Services.' from the key property. – Raul Jun 18 '18 at 12:19
5

As Raul suggested, first replace the Services. from your JSON then create the wrapper class (You can use Json2Apex) and the class will be much simpler as below.

public class ServiceReportWrapper {

    public class ServiceContract_SAM_APIResult {
        public List<Row> row;
    }

    public ServiceContract_SAM_APIResponse ServiceContract_SAM_APIResponse;

    public class ServiceContract_SAM_APIResponse {
        public ServiceContract_SAM_APIResult ServiceContract_SAM_APIResult;
    }

    public class Row {
        public String SR_NBR;
        public String SR_CREATED;
        public String SR_TYPE;
        public String IB_SERIAL;
    }


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

Then replace the Services. dynamically from the response body by using replaceAll() method of String Class before parsing (Example : - res.getBody().replaceAll('Services.','')).

String json = '{"Services.ServiceContract_SAM_APIResponse": {"Services.ServiceContract_SAM_APIResult": {"Services.row": [{"Services.SR_NBR": "1-233391976021","Services.SR_CREATED": "2018-04-26T13:44:25.000Z","Services.SR_TYPE": "Corrective Repair","Services.IB_SERIAL": "[]"}]}}}';
json = json.replaceAll('Services.','');
ServiceReportWrapper obj = ServiceReportWrapper.parse(json);
System.debug(obj);

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.