0

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.

1

1 Answer 1

2

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');
System.debug(attr.get('stock_item'));

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.

You must log in to answer this question.

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