I have created a REST Endpoint (exposed on force.com site) to allow Webhook messages.

The third party service will POST data to the webhook that looks like this:


  "channel": "server",
  "email": "test@example.org",
  "messageId": "test-message-37q7z9",
  "projectId": "oYOQY8rnYw",
  "replay": true,
  "timestamp": "2018-12-04T16:22:37.195Z",
  "traits": {
    "trait1": 1,
    "trait2": "test",
    "trait3": true
  "type": "identify",
  "userId": "test-user-x1nyrr"

How can I read the attributes in the traits object? (i.e. trait1, trait2). I should note the attributes in the traits object may increase/decrease in size so this needs to be somewhat dynamic.

This is what I have so far but I'm not sure how to parse the payload.

global class WebhookReq {

    global static String doPost() {
        //Blob name = RestContext.request.params.get('name');
        String traits = RestContext.request.params.get('traits');
        RestRequest req = RestContext.request;
        String result = JSON.deserialize(req.getBody());

        return result;


Since the types can be mixed, you need to use either the JSONParser (I would not recommend this) or JSON.deserializeUntyped. Here's the latter method:

Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(
Map<String, Object> traits = (Map<String, Object>)params.get('traits');
for(String key: traits.keySet()) {
  Object value = traits.get(key);
  if(value instanceof integer) {
  } else if(value instanceof string) {
  } else if(value instanceof boolean) {

Since I don't know what you intend to do with this, I can't really write more, but at this point, you should have everything you need.


You can use deserializeUntyped(jsonString) here as below. Refer to the example details on the documentation.

Map<String, Object> myMap = (Map<String, Object>) JSON.deserializeUntyped(req.getBody().toString());
Map<String, Object> traitsMap =  (Map<String, Object>)myMap.get('traits');

for(Integer i = 1; i < traitsMap.size(); i++) {
    system.debug(traitsMap.get('traits'+i)); // will print the value of traits<i>

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.