Why the JSON deserializeUntyped giving me Exception

apexjsonmaprest-api

I'm creating a new REST API in salesforce and receiving the below JSON in Restrequest string

{"event_source":"","event_time":"2022-05-25 08:12:40.151","event_value":"{\"ad\":\"\",\"media_source\":\"restricted\"}"}

and when I try to deserialize in map I'm getting below error

DEBUG|Exception : Invalid conversion from runtime type String to Map

I'm using this conversation

Map<String, Object> params = (Map<String, Object>)System.JSON.deserializeUntyped(RestContext.request.requestBody.toString());
Map<String, Object> eventVal = (Map<String, Object>)params.get('event_value');

Best Answer

The decoded parameter is JSON, so you have to deserialize again:

Map<String, Object> params = (Map<String, Object>)System.JSON.deserializeUntyped(
  RestContext.request.requestBody.toString()
);
Map<String, Object> eventVal = (Map<String, Object>)JSON.deserializeUnTyped(
  (String)params.get('event_value')
);

If you can, I'd get that payload changed to something more "normal."

Related Topic