3

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. – geekymartian Sep 17 '14 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 Sep 17 '14 at 22:30
1

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

https://www.salesforce.com/us/developer/docs/dbcom_apex290/Content/apex_System_Json_deserializeUntyped.htm

| improve this answer | |
0

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?

Thanks!

| improve this answer | |
  • 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 Jul 30 '16 at 13:33

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.