Skip to main content

Salesforce - deserialize JSON response in Apex http rest api callout



In my last post - Integrating Salesforce with a HTTP Rest API service , I mentioned I would test out how to take the rest api's json response and convert it to an object using JSON deserialize.

If you are interested to see how to make the apex callout, please see the earlier post.

Now lets modify the earlier apex call to see how to the json string response received from the api and make an object of it. Once it is a object, we can use it to apply any of our business logic on it.

  • First lets take a response of the json response we are receiving -

  •  As you can see we are receiving back an array of json objects. Each object as three attributes - login, firstname and lastname. Now I can create a class in Apex that matches the definition of this json object. This class will be used to convert (deserialize) the json string.

  • I created an inner class "UsersFromApi" which matches the json object and added a constructor for it. Now after I make the API callout and get the result, i will use the JSON.deserialize method to convert the response string to a list of UsersFromApi class objects. 

  • As shown above, after checking the response code is 200, i use the JSON.deserialize method to get an output consisting of an array of UsersFromApi objects. To test this, let me write a quick anonymous code below - 

thats it! now that we have an array, I can loop over it and perform any logic i need such as maybe update some fields on a Salesforce object with the values in the UsersFromApi object.

Hope this was useful for you!


Comments

Popular posts from this blog

Workaround to bypass Salesforce SSO

One of the best practices for implementing Single Sign On for your Salesforce org is always ensure there is a way your System administrator can login via the standard login page side-stepping the SSO configuration.  Reason for this is if ever something goes wrong with your Idp provider's service and the SSO authentication responses are not coming as expected, all your users are unable to login. That is, if you have setup your My domain to prevent logins via standard Salesforce login urls (login.salesforce.com). This includes the System administrator as well. Only if your system administrator can somehow login, then he or she can disable the SSO settings for your domain and allow login via the normal login page as a temporary measure. What do you do in such a situation? Well Salesforce has built a workaround for this which is not well documented anywhere (probably for a good reason :) ). I found out about it from a colleague at work. If your my domain url is - https://Com...

DBAmp for Salesforce - salesforce integration for SQL Server DBAs

Recently i got the opportunity to explore a tool called DBAmp for integration with Salesforce. I found it to be a very useful tool which will help with any data manipulation requirements in Salesforce. Following are my learnings from the exercise. Hope it helps any of you who may need to work with this tool -  DBAmp is a SQL Server package that can be used to integrate with Salesforce. The site where this software is available is - http://www.forceamp.com/ Overview: It essentially installs on top of an existing SQL Server database and provides an OLE DB connector that can be used to connect to Salesforce. Behind the scenes, it executes API calls against Salesforce for any SQL server command it receives. Thus we can create a connection in SQL server to Salesforce org and pull data into tables in the database just as if we are querying against Salesforce tables directly. Use cases for DBAmap + Salesforce: Many use cases exist for using this tool against Sales...

Asynchronous Apex webservice callout - Continuation pattern

In my last two posts, we went through a basic example of consuming an external web service in Salesforce to generate a stub class and then using that stub class to call the webservice from a visualforce page controller. Integrating Salesforce with SOAP webservice - Apex callout Integrating Salesforce with SOAP webservice - Apex callout part 2 In the above example, our callout was synchronous. The user as well as our code (system resources) are going to wait for the webservice to reply and our code to process the response prior to be able to do anything else. If the external webservice responds within 1-2 seconds, no harm done. This should be fine. However there are chances you will encounter webservices in business scenarios that will take longer to respond. This may be because they have to do a significant amount of work prior to responding back to Salesforce. Maybe they process huge number of transactions and during peak loads, their response is slower. All possible scenar...