8

I have observered an strange behaviour on JSON deserialize

As per documentation deserialize(jsonString, apexType)

If the JSON content contains attributes not present in the System.Type argument, such as a missing field or object, deserialization fails in some circumstances. When deserializing JSON content into a custom object or an sObject using Salesforce API version 34.0 or earlier, this method throws a runtime exception when passed extraneous attributes. When deserializing JSON content into an Apex class in any API version, or into an object in API version 35.0 or later, no exception is thrown. When no exception is thrown, this method ignores extraneous attributes and parses the rest of the JSON content.

According above statement, when we deserialize json, any extra field should get ignored.

Below i have include some inValidfield in json and it get parsed without an error. Above statement holds true for parent record,

String jsonString='{"inValidField":"Burlington","Name":"Burlington","Contacts":{"totalSize":1,"done":true,"records":[{"Name":"Jack Rogers"}]}}';
Account acc = (Account) JSON.deserialize(jsonString, Account.class);

where as when i include inValidfield field on child record, it start give me an error

System.JSONException: No such column 'inValidField' on sobject of type Contact

String jsonString='{"inValidField":"Burlington","Name":"Burlington","Contacts":{"totalSize":1,"done":true,"records":[{"inValidField":"Burlington","Name":"Jack Rogers"}]}}';
Account acc = (Account) JSON.deserialize(jsonString, Account.class);

Anyone know how to resolve such issue of parsing ?

5
  • 3
    My instinct tells me this is a bug that needs to be addressed by Salesforce. The documentation is not consistent with the behavior.
    – sfdcfox
    Commented Jul 22, 2021 at 10:22
  • 7
    After checking this internally, I see we have a bug logged #W-6827544 for the scenario that Extraneous attributes in relationship field are not ignored when deserializing JSON. I will follow up with the concerned team and will update this thread as I have more inputs. There is no public-facing KI for this so far. Thanks Commented Jul 22, 2021 at 15:04
  • I have faced a similar issue when sent data from LWC to aura enabled apex method. I had used some additional properties on records for UI state which I did need to save. In that case, it did not even throw an exception and just showed as an internal server error. Commented Jul 22, 2021 at 17:31
  • @rahul-gawale Exactly i have ran into same scenario which you have mentioned.. As LWC does not support inline script , we have to add addtional fields run time inorder resovle display render.
    – NITHESH K
    Commented Jul 23, 2021 at 8:50
  • 1
    @swetha this is much needed, as Rahul mentioned. I have ran into same scenario which he has mentioned.. As LWC does not support inline script , we have to add addtional fields in run time inorder resovle display rendering.
    – NITHESH K
    Commented Jul 23, 2021 at 8:51

1 Answer 1

2

The issue you are facing is related to a bug that is already logged as #W-6827544. I have reached out to our Product Development highlighting your use case and we have created a Known issue.

Link: https://trailblazer.salesforce.com/issues_view?id=a1p4V000002JS0uQAG&title=extraneous-attributes-in-relationship-field-are-not-ignored-when-deserializing-json

While there is no ETA for a fix at the moment, please consider selecting "This Issue Affects me" so that the fix can be prioritized.

You must log in to answer this question.

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