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"
      }
   }
}
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));
| improve this answer | |
  • Thank you for taking time out of your day to explain this, really appreciate it. This solved my issue. – BreadMAN Mar 13 '19 at 12:35

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.