I am getting some data in response to my code which is a JSON string as:

HTTPResponse res = {"id":2048,"extension_attributes":{"stock_item":{"stock_id":1}}}

Now to retrieve data, I've done

Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
Object attr = results.get('extension_attributes');

Here attr is again containing 'stock_item' which finally contains 'stock_id'. Though I am unable to get this by any way from this object. I hope someone would have solution for this.


Each object in your JSON becomes a Map<String, Object> when you deserialize untyped. You just need to cast it as such to be able to use it.

Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
Map<String, Object> attr = (Map<String, Object>)results.get('extension_attributes');

Continue as needed until you can extract the data you want.

At a point, though, it'll make more sense to use an Apex class to deserialize so you don't need to cast things ad nauseum.

| improve this answer | |

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.