1

I have very simple -sample class for the following json which I kept in custom label for simplicity.

Sample JSON Which I have kept in Custom Label for simplicity : Label Name : Smple_Resp

{
    "Details": {
        "Status": "SUCCESS"
    }
}

Class to parse the Json.

public class TestUtil{
public class Details{
    String Status;
}

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

Code which I ran in Workbench

    String testing =label.Smple_Resp;
system.debug(TestUtil.parseDetails(testing));

Debug which I am getting

05:56:27.25 (30728351)|USER_DEBUG|[2]|DEBUG|Details:[Status=null]

Unable to understand what mistake I have done which is returning me NULL value all the time.

3

This is an example of a very common mistake with JSON deserialization: you have an extra layer of structure in your JSON that's not represented in your Apex.

To successfully deserialize, you need an outer structure to correspond to the "Details" key. I would do it like this:

public class DetailsStruct {
    String Status;
}

public class JSONResponse {
     DetailsStruct Details;
}

The object into which you're deserializing has to match the outermost object layer in your JSON.

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.