0

JSON response from the callout is changed.

1) Current JSON response :

{

    XYZ {

    }

    DEF {

    }

    MNO {

   }

}

Now its changed to :

{
   ABC_Stage{
            XYZ{

            }
   }

   DEF{

   }

   MNO{

   }
}

Meaning an extra node is added which is also environment specific and for stage its 'ABC_Stage'. I have kept 'ABC_Stage' in a custom setting and retrieving in the code. Similarly for prod, I will configure 'ABC' in the prod custom setting. Now, my challenge is I want to do minimal/no update in the wrapper class because variables used inside XYZ (which has many other nested nodes)I need to traverse those variables and update its references at many places. Also since the inner structure of XYZ still remains the same. I can think of possible solution as:- Create an another method, pass ABC_Stage (after fetching it from the custom setting) and then remove ABC_Stage { } from the response , so the structure looks similar to current JSON and that won't require any change in the wrapper class while deserializing it. Can someone guide me how to achieve this? or any other possible solutions.

1

Yes, you could make a small method for this:

public static XYZ parse(String jsonString) {
  Map<String, XYZ> results = (Map<String, XYZ>)JSON.deserialize(jsonString, Map<String, XYZ>.class);
  return results.values()[0];
}

This trivializes the changes you'd need to make, as you'd still have XYZ values to work with, and all it really requires is an extra line of code or two in the original parser method.

4
  • I am getting the error - Method does not exist or incorrect signature: void deserialize(String, System.Type) from the type String while saving the class – Manish Anand Apr 24 '19 at 11:20
  • @ManishA You have a variable or class named JSON. Consider renaming it. – sfdcfox Apr 24 '19 at 11:26
  • Thanks @sfdcfox for your response. That error is gone now. I should have made it more clear while posting the question- There are other responses as well apart from XYZ { } , but only XYZ { } needs to be nested inside ABC_Stage{ }. I have updated the current and new responses. If I deserialize it using Map<String, XYZ> , other variables will be null. Any thoughts? Or am I missing something? – Manish Anand Apr 24 '19 at 15:09
  • 1
    @ManishA In that case, you probably want an actual wrapper class: public class Wrapper { public XYZ ABC_Stage; public DEF DEF; public MNO MNO; } Other parsing depends on what you've already got available. I realize you're being "generic" in your question, but that means I can only be generic in my answer as well. – sfdcfox Apr 24 '19 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.