0

I am calling an API from the apex, giving me the following response.

    {
    "status": {
        "error": "no",
        "message": "Successfully updated subscriber"
    },
    "result": {
        "method": "email",
        "_id": "62b1b4d3ca04161d9baf811b",
        "address": "sample.23@gmail.com",
        "meth": "email",
        "statuspage": "62600f07f69a9104dcf7e33e",
        "__v": 0,
        "granular": [
            "62600f07f69a9104dcf7e34e_62600f07f69a9104dcf7e34d",
            "62600f07f69a9104dcf7e34e_627bc9811f8e58052d83ed23",
            "62600f07f69a9104dcf7e34e_627bc98a900216053283ceed",
            "62600f07f69a9104dcf7e34e_627bc9a1772026053b778ef8",
            "62600f07f69a9104dcf7e34e_627bc9aaf4dbda05353fa89b",
            "62600f07f69a9104dcf7e34e_627bc9b372a94b052e01b3f3",
            "62600f07f69a9104dcf7e34e_627bcabc69914405344e7a1a"
        ],
        "active": true,
        "join_date": "2022-06-21T12:08:51.817Z"
    }
}

How can I parse it so I can use the granular as a list of strings?

1

1 Answer 1

0

You can user JSON.deserializeUntyped() to deserialize JSON into Apex collections of primitive values. This will convert JSON into an Object value.

So if you want to get granular List then you can use get method like this:

Map<String, Object> meta = (Map<String, Object>)JSON.deserializeUntyped(jsonString);
Map<String,object> getResult = (Map<String,object>) meta.get('result');
List<Object> granularList = (List<Object>) getResult.get('granular');

Here granularList will have all the data that you want. And if you want the granular List as List<String> then you can convert it like this:

List<string> granularStringList = new List<string> ();
for(Object granularListObj: granularList){
   granularStringList.add(String.valueOf(granularListObj));
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.