0

I have an issue which I've been struggling with today. The JSON is returned back to me with a project Id as the key, this Project Id is dynamically generated and due to this I have not been able to come up with a way to deserialize this response. Any suggestions?

{  
   "success":true,
   "numRows":5,
   "data":{  
      "23000079":{  
         "projectId":"23000079",
         "name":"Mcdonalds",
         "departmentId":"9383",
         "responsibleUserId":"7444434"
      }
   }
}
1

1 Answer 1

2

You'll need to use a Map as part of the data structure you deserialize to account for the dynamic keys. This allows you to avoid hard-coding the keys as part of your defined structure, which you couldn't do here anyway because they're numeric.

public class Response {
    public class Project {
        String projectId;
        String name;
        String departmentId;
        String responsibleUserId;
    }
    Boolean success;
    Integer numRows;
    Map<String, Project> data;
}

This structure works to deserialize the JSON you've shown here in a typed way:

JSON.deserialize(myJSON, Response.class));
1
  • Thank you for taking time out of your day to explain this, really appreciate it. This solved my issue.
    – BreadMAN
    Commented Mar 13, 2019 at 12:35

You must log in to answer this question.

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