0

I have this JSON data From where i have to fetch the "id"`

{
    "_embedded": {
        "users": [{
            "id": "5d54e31886d2db014eefbf45",
            "email": "[email protected]",
            "name": "Custom Developer",
            "active": true,
            "gender": "NotSpecified",
            "extension": null,
            "external_phone_number": null,
            "created_at": "2019-08-15T04:44:08Z",
            "_links": {
                "self": {
                    "href": "https://api.talkdeskapp.com/users/5d54e31886d2db014eefbf45"
                }
            }
        }]
    },
    "count": 1,
    "total": 1,
}

I have tried this code but stucked in parsing between Maps.

HttpResponse httpRes = httpToken.send(httpReq);
Map<String, Object> jsonResponse = new  Map<String, Object>();
String resultgetuserid = String.valueOf(httpres.getBody());
System.debug('Response' + resultgetuserid);
jsonResponse = (Map<String, Object>)JSON.deserializeUntyped(httpres.getBody());
System.debug('Response : ' + jsonResponse);
Map<String, Object> myMaps = new Map<String, Object>();
List<Object> myMapObjects = (List<Object>) jsonResponse.get('_embedded');
//System.debug('myMaps' + myMaps);
String userId = ''; // to be retrieved
System.debug('User ID: ' + userId);
return userId;  

Thanks in Advance.

1

1 Answer 1

2
HttpResponse httpRes = httpToken.send(httpReq);
        Map<String, Object> jsonResponse = new  Map<String, Object>();
        jsonResponse = (Map<String, Object>)JSON.deserializeUntyped(httpres.getBody()); //GET JSON
        System.debug('Response : ' + jsonResponse);
        Map<String, Object> myMaps = new Map<String, Object>();
        myMaps = (Map<String, Object>)jsonResponse.get('_embedded');// FILTER EMBEDDED
        System.debug('myMaps: ' + myMaps);
        List<Object> myMapObjects = (List<Object>) myMaps.get('users'); // SINCE USERS IS LIST store it in list
        Map<String, Object> Users = new Map<String, Object>();
        Users = (Map<String,Object>)myMapObjects[0]; //ASSIGN USERS LIST TO A fresh MAP
        System.debug('User map : ' + Users);
        String userId =String.valueOf(Users.get('id'));// THERE U HAVE THE ID
        System.debug('User ID: ' + userId);
        return userId;  

This will do the magic......Thanks.

1
  • its really impressive. Thanks a lot.
    – Aishwarya
    Sep 19, 2019 at 7:23

You must log in to answer this question.

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