Very new to Apex (first post here!)

I'm making an HTTP callout and receiving a JSON object formatted as such:

{"suggestions":{ "employees": [{"first_name":"John","last_name":"Doe","specialty":["IT", "Development"]}], "facilities": [{"facility_name":""Facility A"}]}}

I used heroku to generate the following APEX class to handle to object:

public class MyClass {

public class Suggestions {
    public List<Employees> employees;
    public List<Facilities> facilities;

public class Facilities {
    public String facility_name;

public Suggestions suggestions;

public class Employees {
    public String first_name;
    public String last_name;
    public List<String> specialty;

I then used the following line in a method to do the parsing, where "json" is the JSON string returned from the callout:

(MyClass) System.JSON.deserialize(json, MyClass.class);

This works beautifulls when the "specialty" value returns a list of specialties. However, when a single String is returned (i.e., the employee only has one specialty), I am given a "Expecting List instead encountered...", which I'm assuming means it encountered a String instead of the expected List.

Is there a way to set up the class so that it can parse both data types? Or is there a way to check the data type of the value for "specialty" within .deserialize(), and insert some logic to change all Strings to Lists or vice versa?

Thanks for any help in advance!

  • 2
    I don't think it's possible to do that evaluation from the class. Since you can have different structures I think the best is to stick with JSON.deserializeUntyped() and deal with the maps generated. Commented Sep 17, 2014 at 20:45
  • 1
    If you have any ability to influence the code that emits the JSON, always returning a JSON array (with zero, one or more elements) is a more sane approach rather than changing from an array to a single value.
    – Keith C
    Commented Sep 17, 2014 at 22:30

2 Answers 2


You can try something like below

Map<String, Object> testMap = (Map<String, Object>) JSON.deserializeUntyped( jsonStr );

There is a detailed code example in the below url



Apologies in advance if I'm not going about it the right way, but I posted as a guest before I made the account. Tried to comment under your suggestion urubrx and Keith C but didn't have the rep.

I also experimented with your suggestion of converting to map (thanks for the replies by the way!). However, I had difficulty getting any key-value pairs out of the map. For example, where "m" is the map generated from the line of code you offered, m.size() is = 1, where the only K-V pair is "suggestions"="[entire remainder of the JSON object]. is there a way to dive deeper into the map?


  • The OP is not a registered user and the above user appears to be the OP, who couldn't comment or edit the original post. For that reason, I recommend this post not be deleted. From Review Queue.
    – crmprogdev
    Commented Jul 30, 2016 at 13:33

You must log in to answer this question.

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