Salesforce: Serialize JSON Object with APEX
How do you serialize a more complex JSON object with APEX? Join Cheyenne as she shows you how!
…and while you’re here: if you’re having Salesforce user adoption challenges, we have a brand new guide that takes you step by step through the principles of teaching adults technology, and drives you to develop a plan for implementation. It’s got room for you to make your own plan – check it out: https://cloudadoption.solutions/teach…
Contact us with your Salesforce challenges at email@example.com – we love to help!
Cloud Adoption Solutions is a 100% woman-owned registered Salesforce partner, specializing in implementation, integration, and optimization for Technology, Healthcare/ Life Sciences, and Financial Services/ Professional Services organizations in the small and mid-commercial sectors.
Hi, this is Cheyenne with Cloud Adoption Solutions. And today, I’m going to show you how to serialize a more complex JSON object with apex. So in this example request, we need to build this with apex and serialize it into JSON. Here, we have an outer JSON object and an inner JSON object in this one request body. So I’m going to show you how to set the outer fields in the inner fields with apex and then serialize them into one JSON request body. So first, we need to create an apex wrapper class containing our JSON body fields. So for each JSON object we have in our body, there needs to be an inner Apex class in your wrapper. So this outer Apex class, named example request would correspond to our outer JSON object, which includes these fields. And our inner Apex class called payment would correspond with our inner JSON object called payment.
Okay, so once we have our wrapper class built, then we need to use them to construct a request. The first thing we’ll want to do is initialize those Apex classes. So example request is our outer Apex class that I’m calling outer object. And then we will also need to initialize our inner Apex class via dot notation, example request dot payment. And here I’m calling this inner object. The next thing we’ll need to do is set those inner set those fields that will eventually be the inner JSON object fields. So we will take our inner Apex class, use dot notation to access the fields and assign them a value. So in our wrapper class, inner object dot token corresponds with our inner class payment, and the field token. Okay, and then the next thing we need to do once we’re done assigning all of our inner object fields, we need to associate that inner class that we just built with our apex outer class. The example request, so I will take the instance of my Apex outer class called out or object and use dot notation. To assign it the instance of the inner Apex class that we just created in the ones above. And then we can set our fields in for our outer Apex class that will eventually be our outer JSON object. So outer object dot amount would correspond in our wrapper to the example request and the amount and then once we are done setting our fields for the outer object, then we will create an HTTP HTTP request. And set the body need to use and use JSON dot serialize the name of our outer class Apex instance called outer object and that’s all there is to it.