0

I'm trying to parse a nested JSON through a REST Api but somehow it is not getting read. For example: I'm passing a JSON like this in my request body.

[
{
"first":"I like sfdc",
"second":"You like sfdc",
"third": "I want an apple",
"fourth": [
           { "ISDETAIL":false,
            "SERIAL":"X62348052228",
            "TYPE":"NEWKING",
            "STATE":"DKS"
            }
          ]
}
]

I am getting the rest response through

String jsonString = req.requestBody.toString();

This jsonString returns all the value in debug.

I try to write a wrapper class for the main json response like

global with sharing class RestWrapperELH {

    global class JSONRestResponse{
        global String first;
        global String second;
        global String third;
        global JSON fourth;
    }
}

And then I'm deserializing the json using this code

List<RestWrapperELH.JSONRestResponse> restPayload = (List<RestWrapperELH.JSONRestResponse>)JSON.deserialize(jsonString, List<RestWrapperELH.JSONRestResponse>.class);

But on debugging the restPayload, I'm getting a output like

21:19:28:014 USER_DEBUG [24]|DEBUG|(JSONRestResponse:[first=I like sfdc, second=You like sfdc, fourth=JSON:[], third=I want an apple])

I understand that I have to deserialize the nested JSON, I have tried writing a wrapper class around it using https://json2apex.herokuapp.com/ but I still can not work with it because the JSONRestResponse.fourth is always null.

What is it that I'm doing wrong ?

2
  • I believe this could be the reason global JSON fourth;. Since we've a standard class named JSON & the member of the class doesn't match the type you're receiving. Renaming the inner class you created to something else should fix this. Dec 7, 2022 at 6:15
  • @SachinHooda I do not understand what you mean. I wanted to parse the fourth as JSON so I set the data type as that. But I understand now that I would be needing to deserialize/serialize to get the optimal results Dec 8, 2022 at 6:10

1 Answer 1

0

If you use https://json2apex.herokuapp.com/ this works fine.

The converted class is

public class JSON2Apex {


    public String first;
    public String second;
    public String third;
    public List<Fourth> fourth;

    public class Fourth {
        public Boolean ISDETAIL;
        public String SERIAL;
        public String TYPE;
        public String STATE;
    }

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

Which easily shows the wrapper class when you try to pass your JSON.

System.debug(JSON2Apex.parse('[\n' +
        '{\n' +
        '"first":"I like sfdc",\n' +
        '"second":"You like sfdc",\n' +
        '"third": "I want an apple",\n' +
        '"fourth": [\n' +
        '           { "ISDETAIL":false,\n' +
        '            "SERIAL":"X62348052228",\n' +
        '            "TYPE":"NEWKING",\n' +
        '            "STATE":"DKS"\n' +
        '            }\n' +
        '          ]\n' +
        '}\n' +
        ']'));

Output as :

09:58:17.218 (233731852)|USER_DEBUG|[4]|DEBUG|(JSON2Apex:[first=I like sfdc, fourth=(Fourth:[ISDETAIL=false, SERIAL=X62348052228, STATE=DKS, TYPE=NEWKING]), second=You like sfdc, third=I want an apple])

Fourth as list.

1
  • For some reason, my org started giving me the desired results the next day. Thanks for your explanation. Marking this as correct. Dec 8, 2022 at 6:12

You must log in to answer this question.

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