Using JSON in WebSphere Cast Iron, Part 2: Invoking JSON-based REST APIs

IBM® WebSphere® Cast Iron introduced two activities, Read JSON and Write JSON to work with JSON data. Part 1 of this 3-part series showed how to configure JSON handling activities to invoke JSON-based REST APIs and to create your own. Part 2 shows how to build an orchestration that invokes JSON-based REST APIs exposed by Salesforce.com® to create an account.

Share:

Rajath Ramesh (rajathr1@in.ibm.com), Software Engineer, IBM

Photo of Rajath RameshRajath Ramesh is a Software Engineer at the IBM India Software Labs. He started his career with IBM in 2010 as a developer in WebSphere Adapters and incubation projects. He is currently part of the WebSphere Cast Iron development team, specializing in Cast Iron Connectors, Cast Iron Studio, and the Connector Development Kit. He is a co-author of IBM Redbook: Getting Started with IBM WebSphere Cast Iron Cloud Integration.



Manjunatha D (manjunad@in.ibm.com), Software Engineer, IBM

Photo of Manjunatha DManjunatha D is a Software Engineer at the IBM India Software Labs. He works with the IBM WebSphere Adapters, IBM WebSphere Cast Iron, and IBM API Management teams.



31 July 2013

Also available in Chinese

Introduction

WebSphere Cast Iron (hereafter called Cast Iron) is widely used in data integration and migration across on-premise and off-premise applications. In the past, most of the SaaS-based applications expose REST APIs, which work with JSON data. However, Cast Iron mainly works with XML and XML schemas. To enable effective invocation of REST APIs, Cast Iron has introduced two new activities to work with JSON data.

You can use the Read JSON and Write JSON activities to create JSON request and parse JSON response during the invocation of REST APIs, which are mainly exposed by SaaS providers. In Part 2, you will learn an effective method to invoke a Salesforce.com® REST API to create an account record.


Invoking a Salesforce.com API

In this scenario, you must invoke a Salesforce.com REST API to create an Account, which accepts input and sends a reply data in the JSON format. A sample JSON request and response of the Salesforce.com REST API is shown in Table 1.

Table 1. Salesforce.com account - sample JSON request and response
Request JSONResponse JSON
{
"name" : "DeveloperWorksAccount"
}
{
"id" : "1QPK2343877887783PS",
"success" : true,"errors" : [ ]

}

While building a complex orchestration, the mentioned input name can be mapped across variables, which eventually must be sent as part of the JSON Request data. Similarly, a JSON Response received from Salesforce.com API must be easily available to map for effective development of the orchestration.

In the orchestration shown in Figure 1, "name" is passed as an HTTP body to the Receive REST Request activity and it is consumed by the Write JSON activity to form the JSON Request for the Salesforce.com API. On the other hand, the output of the Salesforce.com API invocation is consumed by the Read JSON activity, which provides output data in XML format for easy and effective mapping across the orchestration.

Figure 1. Create a Salesforce.com account orchestration
Create a Salesforce.com account orchestration
  1. The Receive REST Request (Receive Request) activity receives the text in the body variable. The value of "name" will be used to create an account in Salesforce.com. Table 2 shows the steps to complete Receive REST Request activity's checklist.
Table 2. Receive REST Request activity
Checklist itemDescription
Pick Endpoint Create and configure the new HTTP endpoint and select the HTTP endpoint during configuration.
Configure Input /createaccount as the URL (path after host name) and select Any method for the HTTP method.
Map Output Create a new variable accountname based on the output parameters of the activity. You can use this variable as the input variable while configuring the Write JSON activity.
  1. The Get SDFC Session ID (Invoke Service) activity logs on to Salesforce.com using the Web service exposed and extracts the Session ID for further invocations to Salesforce.com. Table 3 shows steps to complete the Get SDFC Session ID Activity's checklist.
Table 3. Get SDFC Session ID activity
Checklist itemDescription
Pick Endpoint Create and configure the new Web Service endpoint with the Salesforce.com WSDL and select that endpoint at the configure step.
Configure Select the login Operation from the WSDL operation list.
Map Input Specify the user name and password.
Map Output Create a variable loginresponse by copying the body variable from the orchestration.
  1. Create the JSON Request activity (Write JSON) provides a "name" that is received as part of the body variable in the JSON format. In this scenario, you will be using an XML schema, as shown in Listing 1, to configure the JSON activity. Add the XML Schema to the Cast Iron Studio project to configure the Create JSON Request (Write JSON) activity. Select Select XML Schema as the message type from the Configure window, which is shown in Figure 2.
    Listing 1. Account name XML Schema
    <?xml version="1.0" encoding="UTF-8"?>
    <schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
      targetNamespace="http://www.approuter.com/schemas/RootNode" 
      xmlns="http://www.w3.org/2001/XMLSchema"> <element name="name" 
      nillable="false">
     <complexType>
     <sequence>
     <element maxOccurs="1" minOccurs="0" name="name" nillable="false" type="string"/>
     </sequence>
     </complexType>
     </element>
    </schema>
    Figure 2. Create the JSON Request activity
    Create the JSON Request activity
    1. Once you enable the Select XML schema option, the Select XML schema button is activated as shown in Figure 2. Click the Select XML Schema button to browse the XML schema, then select the XML schema node and click OK. You can see the selected XML schema node as a Map Inputs parameter in Figure 3.
    2. Enable the Remove Root Element checkbox to remove the virtual root element from the JSON result to match the request format (structure shown in Table 1).
    3. Click the Map Input checklist item, and then click the Select Input... button to select the input variable accountname, which was created while configuring the HTTP Receive Request activity. Map from accountname to the name field as shown in Figure 3.
      Figure 3. Create the JSON Request Activity Map Inputs
      Create the JSON Request Activity Map Inputs
    4. Click the Map Output and copy the JSON text to create a new variable jsonRequest, as shown in Figure 4. You can use this jsonRequest to configure the next activity.
      Figure 4. Create the JSON Request Map Outputs
      Create the JSON Request Map Outputs
  1. Now add and configure the Create SFDC Account (HTTP Post Request) activity, which creates a Salesforce.com account. You have a Salesforce.com session ID generated as output of the Invoke Service and JSON message. Configure the HTTP Post activity to create an account in Salesforce.com using the Session ID and JSON message. Then configure the HTTP Post Request with the following details shown in Table 4 to invoke SFDC to create an account.
Table 4. Create an SFDC account activity
Checklist itemDescription
Pick Endpoint Create and configure the new HTTP endpoint. Select the HTTP endpoint at the Configure step.
Configure Add the Authorization header to the custom header.
Map Input
  1. Select the variable loginReponse, concatenate the "Bearer" and Session ID, and then map the concatenated value to the Authorization custom header.
  2. Select the variable jsonRequest and map the selected variable to the body of the HTTP Post activity.
Map Output Create a variable CreateAccountResponse by copying body.
  1. Now add and configure the JSON response to XML activity (Read JSON). "CreateAccountResponse" is in the JSON format and it has to be converted to an XML variable for further mapping of the response in the orchestration. You can configure the Read JSON as explained below. As you are using the XML Schema to configure JSON activities, add the XML Schema result, as shown in Listing 2, to the project.
    Listing 2. Create an account response XML schema
    <?xml version="1.0" encoding="UTF-8"?>
    <schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
       targetNamespace="http://www.approuter.com/schemas/RootNode" 
       xmlns="http://www.w3.org/2001/XMLSchema">
     <element name="Result" nillable="false">
     <complexType>
     <sequence>
     <element maxOccurs="1" minOccurs="0" name="id" nillable="false" type="string"/>
     <element maxOccurs="1" minOccurs="0" name="success" nillable="false" type="string"/>
     <element maxOccurs="1" minOccurs="0" name="errors" nillable="false" type="string"/>
     </sequence>
     </complexType>
     </element>
    </schema>
    1. Click the Select XML Schema message type from the Configure window as shown in Figure 5.
      Figure 5. Convert the JSON response to an XML activity
      Convert the JSON response to an XML activity
    2. Once you enable the Select XML schema option, the Select XML schema button is activated as shown in Figure 5. Click the Select XML Schema button to browse the XML schemas, then select the XML schema node and click OK. You can see the selected XML schema nodes as the Map Inputs parameter in Figure 5.
    3. Click on the Map Inputs checklist item, click on the Select Inputs… button, then select the input variable CreateAccResponse, which is created while configuring the HTTP Post Activity. Then map CreateAccResponse to JsonText as shown in Figure 6.
      Figure 6. Convert the JSON response to XML Activity Map Inputs
      Convert the JSON response to XML Activity Map Inputs
    4. Click Map Outputs in the Checklist pane and copy the JSON text to create a new variable XMLResult, as shown in Figure 7.
      Figure 7. Convert the JSON response to XML Activity Map Outputs
      Convert the JSON response to XML Activity Map Outputs

You can use this XMLResult to effectively map the result data across an orchestration in future activities.


Testing the orchestration

Now you have developed an orchestration that accepts an account name and uses the same to create an Account record in Salesforce.com using the REST API exposed by Salesforce.com.

To test the orchestrations, complete the following steps:

  1. Click the Project tab, right-click on the CreateSFDCAccount orchestration name, and click Start Orchestration as shown in Figure 8.
    Figure 8. Start the CreateSFDCAccount Orchestration
    Start the CreateSFDCAccount Orchestration
  2. Click the Tools menu and select the HTTP Post Utility to invoke the API. The HTTP Post window appears. Type DeveloperWorksArticle in the "Post a message" text area. Specify the URL configured in the HTTP Receive Request Activity, and then select the method POST. Click the Submit button to post the request as shown in Figure 9.
    Figure 9. Invoke using the HTTP Post Utility
    Invoke using the HTTP Post Utility
  3. On successful completion of the orchestration, you can validate if the account is created in Salesforce.com. Figure 10 shows that the account record with the name "DeveloperWorksArticle" is created in Salesforce.com.
    Figure 10. Created account in Salesforce.com
    Created account in Salesforce.com
  4. The XMLResult variable holds the data of the JSON response, which is received as part of the Salesforce.com account creation invocation. Figure 11 shows XMLResult, which can be used for further mapping across activities of that orchestration.
    Figure 11. XMLResult variable holding the JSON response data
    XMLResult variable holding the JSON response data

You have successfully created an account in Salesforce.com using the JSON-based REST API by effectively handling JSON data in Cast Iron. In Part 3, you will learn how to design an orchestration that is exposed as a JSON-based API.


Conclusion

In Part 2 of this series, you learned how to use the Read JSON and Write JSON activities in WebSphere Cast Iron to effectively handle JSON data during an invocation of a REST API. This article stressed the usage of existing XML schemas while configuring the Read JSON and Write JSON activities. In Part 3, you will learn how to design an orchestration that is exposed as a JSON-based API. An invocation of a Salesforce.com API is illustrated, which is a similar method that you can use to invoke other JSON-based REST APIs.

Acknowledgments

The authors would like to thank Raghavendra K. Neelekani for reviewing and providing valuable suggestions for this article series.

Resources

Learn

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=939304
ArticleTitle=Using JSON in WebSphere Cast Iron, Part 2: Invoking JSON-based REST APIs
publish-date=07312013