function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian11Brian11 

What is JSON.deserializeUntyped() doing?

I recently Used this in my Apex Class and I ran into a weird problem.

From my lightning controller I send the following data as a string format using JSON.Stringify
{ 
 item: "4",
 change:  "40",
 limit: "400"
}

On my apex Class controller I used JSON.deserializeUntyped() to map it to an object
Map<String, Object> listJson = (Map<String, Object>) JSON.deserializeUntyped(stringList);

I wasnt working at all Until I wrapped the data coming from the lightning controller in an object so when I did the following then the JSON.deserializeUntyped() worked perfectly. 
 
{
 data:  { 
  item: "4",
  change:  "40",
  limit: "400"
 }
}

Can someone explain why this is necessary. Or is there a solution that better

 
Best Answer chosen by Brian11
Alain CabonAlain Cabon
Hi,
String jsonInput = '{"item": 4, "change":  "40", "limit": 400}';
    
Map<String, Object> m =   (Map<String, Object>) JSON.deserializeUntyped(jsonInput);

Integer a =  (Integer)m.get('item');
System.debug('item:' + a); 

String c = (String)m.get('change');
System.debug('change:' + c);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm#apex_System_Json_deserializeUntyped