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
Victor DomtazVictor Domtaz 

Apex JSON.deserializeUntyped

Hi, 
I'm using JSON.deserializeUntyped method, I tested it through dev console in 2 unrelated orgs and both of them gave the same strange result.
I run this code through anonymous window, which basically takes a JSON and through deserializeUntyped method, breaks it to pieces: 

string ex='{"Quote Line_Repeating":[{"Quote Line":{"Line ID":"1","Discount":"true","Discount Type":"Regular","Sell Multiplier":1,"Rep Net Multiplier":0.75,"Pricing":"true","Base List Price":866,"Base Currency":"USD","Quote List Price":866,"Quote Currency":"USD","Exchange Rate":1}}]}';

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(ex);

List<Object> a = (List<Object>)m.get('Quote Line_Repeating');System.debug('ex: '+ex);System.debug('m: '+m);
 


 The result that I got for ex variable:

USER_DEBUG [6]|DEBUG|ex: {"Quote Line_Repeating":[{"Quote Line":{"Line ID":"1","Discount":"true","Discount Type":"Regular","Sell Multiplier":1,"Rep Net Multiplier":0.75,"Pricing":"true","Base List Price":866,"Base Currency":"USD","Quote List Price":866,"Quote Currency":"USD","Exchange Rate":1}}]}
 

The results which I got for m variable:

USER_DEBUG [7]|DEBUG|USER_DEBUG [7]|DEBUG|m: {Quote Line_Repeating=({Quote Line={Base Currency=USD, Base List Price=866, Discount=true, Discount Type=Regular, Exchange Rate=1, Line ID=1, Pricing=true, Quote Currency=USD, Quote List Price=866, Rep Net Multiplier=0.75, ...}})}
 

If you compare carefully the 2 logs, you'll see that after going through JSON.deserializeUntyped method, the "Sell Multiplier":1 value just vanished from the view and the log shows
It's seems that the 'deserializeUntyped' sorted the attributes and omitted all attributes after the 10th attribute. 

In addition, I tweaked the JSON by adding and removing attributes and always the result was the same, the first 10 alphabetical attributes appeared and the other were omitted.
I think that it's a bug, as I didn't find any documentation about this kind of SF limit, but maybe I'm missing something.
I'll be happy for assistance Thanks
Victor

Best Answer chosen by Victor Domtaz
Maharajan CMaharajan C
Hi Victor,

Try the below code to get the Sell Multiplier from JSON:
 
string ex='{"Quote Line_Repeating":[{"Quote Line":{"Line ID":"1","Discount":"true","Discount Type":"Regular","Sell Multiplier":1,"Rep Net Multiplier":0.75,"Pricing":"true","Base List Price":866,"Base Currency":"USD","Quote List Price":866,"Quote Currency":"USD","Exchange Rate":1}}]}';
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(ex);
List<Object> a = (List<Object>)m.get('Quote Line_Repeating');
System.debug('ex: '+ex);
string b = '';

for (Object obj : a ) {
Map<String, Object> ca = (Map<String, Object>)obj;
Map<String, Object> ql = (Map<String, Object>)ca.get('Quote Line');
system.debug(' b --> '+ ql.get('Sell Multiplier') );
b = String.valueof(ql.get('Sell Multiplier')) ;
}

system.debug(' b --> '+ b );

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Victor,

Please find the below document where you can find the reason why salesforce truncating the large strings with dots.
https://releasenotes.docs.salesforce.com/en-us/spring19/release-notes/rn_forcecom_developer_console.htm

You can find the more expainations from below links:
https://salesforce.stackexchange.com/questions/116502/console-log-truncates-my-debug
https://salesforce.stackexchange.com/questions/253381/system-debug-messages-get-trimmed/253774#253774

Use the below ways to display the entire string.
 
string ex='{"Quote Line_Repeating":[{"Quote Line":{"Line ID":"1","Discount":"true","Discount Type":"Regular","Sell Multiplier":1,"Rep Net Multiplier":0.75,"Pricing":"true","Base List Price":866,"Base Currency":"USD","Quote List Price":866,"Quote Currency":"USD","Exchange Rate":1}}]}';

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(ex);

List<Object> a = (List<Object>)m.get('Quote Line_Repeating');
System.debug('ex: '+ex);

System.debug('m: '+m);

system.debug(' m: ==> ' + JSON.serializePretty(m));

System.debug(' m: ==> ' + JSON.serialize(m));

Thanks,
Maharajan.C
Victor DomtazVictor Domtaz

Maharajan, maybe it's my fault and I didn't explain my problem well enough.
It's not only that dev console is trimming the string because it's too long and I can find the full log through Setup->Debug logs
The string is being actually trancated, for real (!) and the 11th attribute is not available for further logic.

if you execute this code, you would expect to get for the last line the value of '1', but instead you're getting nothing

 

string ex='{"Quote Line_Repeating":[{"Quote Line":{"Line ID":"1","Discount":"true","Discount Type":"Regular","Sell Multiplier":1,"Rep Net Multiplier":0.75,"Pricing":"true","Base List Price":866,"Base Currency":"USD","Quote List Price":866,"Quote Currency":"USD","Exchange Rate":1}}]}';
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(ex);
List<Object> a = (List<Object>)m.get('Quote Line_Repeating');
System.debug('ex: '+ex);
string b=string.valueof(a);
System.debug('m: '+m);
System.debug('a: '+a);
System.debug('b.Sell Multiplier: '+b.substringAfter('Sell Multiplier'));
Maharajan CMaharajan C
Hi Victor,

Try the below code to get the Sell Multiplier from JSON:
 
string ex='{"Quote Line_Repeating":[{"Quote Line":{"Line ID":"1","Discount":"true","Discount Type":"Regular","Sell Multiplier":1,"Rep Net Multiplier":0.75,"Pricing":"true","Base List Price":866,"Base Currency":"USD","Quote List Price":866,"Quote Currency":"USD","Exchange Rate":1}}]}';
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(ex);
List<Object> a = (List<Object>)m.get('Quote Line_Repeating');
System.debug('ex: '+ex);
string b = '';

for (Object obj : a ) {
Map<String, Object> ca = (Map<String, Object>)obj;
Map<String, Object> ql = (Map<String, Object>)ca.get('Quote Line');
system.debug(' b --> '+ ql.get('Sell Multiplier') );
b = String.valueof(ql.get('Sell Multiplier')) ;
}

system.debug(' b --> '+ b );

Thanks,
Maharajan.C
This was selected as the best answer
Victor DomtazVictor Domtaz
TY Maharajan, your code helps, any idea why the method that I used didn't do the job?