Skip to main content

Hi, 

A tough question, 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 

only 3 dots. It's seems that the 'deserializeUntyped' sorted the attributes and omitted all attributes after the 10th attribute. In addition, I tweak 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

1 risposta
  1. 3 lug 2021, 20:35

    Hi Victor, 

     

    The "Sell Multiplier":1 is not getting vanished, it's still there. Those 3 dots at the end means that the line was too long to display, hence got truncated after a certain number of characters.

    This was mentioned in the release notes of Spring' 19 [ https://releasenotes.docs.salesforce.com/en-us/spring19/release-notes/rn_forcecom_developer_console.htm ].

     

    To overcome this truncation limit, use the JSON.serialize method inside the System.debug method.

    Example Code:

    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(no-trucation): ' + JSON.serialize(m)); // prints the full object with all the keys visible

    Resulting Logs:

    USER_DEBUG|[4]|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}}]}

    USER_DEBUG|[5]|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, ...}})}

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

    Output of Line no. 6 prints the complete object with all the keys visible including the key "Sell Multiplier":1.

     

    Reordering of keys:

    It is observed in the output above, that in Line no. 5, the keys are sorted in an alphabetical order. While on Line no. 6, the same keys are now sorted in reverse of their original order(i.e. Line no. 4).

    Hence the ordering of keys is not guarantied to be same. Also the JSON or Maps are considered to be an un-ordered collectionof key-value pair. So we must never rely upon the ordering of their keys.

     

    If some use case demands the ordering to remain constant, then one can make use of a List in Apex. Because they are an ordered collection of elements.

     

    Do you need those keys to be in a particular order? If yes, then kindly mention your use case, because I can't think of any as of now.

     

    Regards

    Manish

0/9000