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

Salesforce Big Objects - Key learnings

I remember reading about Salesforce Big Objects before they became available and thought maybe it is an option to backup data present in regular objects. That is, instead of taking a backup via an ETL tool or data loader, maybe this will be an option to backup the data in the Force.com platform itself. Now that it is GA and I am reading more about it, i think the use cases for this are more varied. Some use cases I can think of are –  Archival of key data objects within the platform: You may want to use this option if you dont use any other means to backup key data. Also this may be an attractive option for non-large enterprise customers who dont themselves invest on large data warehouses or data lakes in their enterprise architecture. Ex: customer history (if present in tasks and activities) will become huge over years but this is useful information for reporting & customer analysis. Store key information which is large volume in quantity and also high volume in transa

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 Salesforce. Pr