1

Given the next code:

    Object deserializeResponse = JSON.deserializeUntyped(response);
    System.debug('DESERIALIZERESPONSE' + deserializeResponse);
    //DESERIALIZERESPONSE: {proposalIds=null, response=true}
    System.assert(deserializeResponse.response ==true);

I need to access to response property of the deserializeResponse Object. How can I do it? Because, right now, I have the next compilation error: Variable does not exist: response

2 Answers 2

3

You are using deserializeUntyped, that means, deserializeResponse as Object does not know about its structure during compilation. Try this approach:

Map<String, Object> deserializeResponse = (Map<String, Object>) JSON.deserializeUntyped(response);
System.assert((Boolean) deserializeResponse.get('response'));
2
  • Simple but effective. Thank you so much!
    – CPS
    Dec 10, 2021 at 12:56
  • 2
    The Object knows about its structure at runtime, but the compiler does not know at compile-time, hence the need to cast.
    – sfdcfox
    Dec 10, 2021 at 12:58
3

The Object type is not really something we can use directly in Apex. I don't believe it has any fields/properties, and only has a few known (but undocumented) methods.

The generally preferred approach to deserializing JSON in Apex is to create a series of classes that match the structure of the JSON you're trying to deserialize.

In this case, that would look something like this:

public class MyJsonData{
    public List<Id> proposalIds;
    public Boolean response;
}

// Even though we're telling JSON.deserialize() to use MyJsonData as a template
//   of sorts, it always returns an Object, so we need to do a little type-casting
MyJsonData data = (MyJsonData)JSON.deserialize(response, MyJsonData.class);

// When working with booleans, there is no need to test for equality to true
//   (or false, either).
System.assert(data.response);

JSON.deserializeUntyped() is generally used when your JSON data contains something problematic for Salesforce like a reserved word (break, continue, switch) or things like __c.

deserializeUntyped() also returns an Object, but it actually is either a Map<String, Object> or a List<Object>, depending on whether the topmost element in the JSON is a (JSON) object or a list. In your case, you'd get a Map<String, Object>.

Using a Map<String, Object> can involve a lot of extra type-casting (which is why at least I prefer defining classes for the deserialization), but in your case would be fairly simple.

Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(response);

Boolean jsonResponse = (Boolean)data.get('response');

System.assert(jsonResponse);
2
  • 1
    So instructive, I've learned a little bit more about Object, Serialize, JSON, and so on... Thank you!
    – CPS
    Dec 10, 2021 at 12:57
  • As you say, Object has no properties, and two methods, equals and hashCode. JSON.deserializeUntyped can also return a String, Boolean, Decimal, or null value as well. Overall, nice work, though.
    – sfdcfox
    Dec 10, 2021 at 12:57

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.