Using the JSON parser to convert the response from an HTTP call for subsequent use in a flow
IBM® App Connect includes a JSON parser that you can use to parse the output from an HTTP invoke action or another application in a flow. If data is returned as a JSON string, the key and value pairs that are encoded in the string are not recognized as separate entities. Therefore, you must use the JSON parser to convert the string into a JSON object to make the data accessible for subsequent use.
Scenario
You work for the Electoral Commission Office in the idyllic state of Utopia, a fictional country. To prepare for the upcoming elections, your department provides a form on the Electoral Commission website so that residents can register to vote online. When each voter registers, you want to send them an email that confirms the receipt. You also want to send them details about their local candidates and a URL reference that they can use to obtain up-to-date election information.
![Conceptual flow for the scenarios Conceptual flow for the scenarios](../files/wufoo-export.png)
- Free or business accounts for Wufoo and Gmail.
- An online form that voters can use to provide their name and address details. For the scenario in this tutorial, assume that the form is provided as an embedded Wufoo form on the Electoral Commission website.
- Access to a web API with a publicly exposed HTTP endpoint that returns election data that is relevant to a voter's postcode.
Connecting to your applications
- For Wufoo, you need the API key and the subdomain for your Wufoo account. For more information, see How to use IBM App Connect with Wufoo.
- For Gmail, you need an application client ID and client secret and for OAuth authentication, an access token and refresh token for you Gmail account. For more information, see How to use IBM App Connect with Gmail.
In this tutorial, the API key is included as a parameter in the URL that is constructed to call the API. An alternative method to enable API key authentication is to add details about the API key parameter, value, and location in the HTTP account connection fields. For more information, see the "What to consider first" section in How to use IBM App Connect with HTTP.
Creating the flow
The following steps describe how to create an event-driven flow with the name Send
election data to registered voters
. The flow uses Wufoo as the source application with a New form entry event. In this scenario,
voters submit the form to trigger the flow. (For more information, see Creating an event-driven
flow.)
App Connect automatically saves your changes as you go. If you move away from the flow at any point, the flow is saved and you can come back to later.
- Create an event-driven flow in one of the following ways.
- On the App Connect Designer home page, click Create an event-driven flow.
- On the App Connect Designer dashboard page, click .
- Enter a name that identifies the purpose of your flow, such as Send election data to registered voters.
- Select Wufoo as your first application (source), then select the New form entry as the event to trigger the flow.
- For Form, select the form that voters submit, such as
Electoral Commission of Utopia: Registration.
Figure 2. Wufoo New form entry event that shows the selected form - Select HTTP as your first target application, then click
Invoke method as the action that is completed when the flow is triggered. In
this scenario, the HTTP connector uses a GET method to look up election data on an HTTP endpoint
that is based on the postcode in the submitted form.
- Before you created the flow, you already configured access to this endpoint by enabling the API and generating an API key. Therefore, you need to specify the URL and append the API key, voter's address, and election identifier as query parameters.
- You also use the Request headers field to indicate that the content type of the response must be JSON. (After you add the HTTP node to the flow, show the Request headers field by clicking Edit mappings for the Request headers heading.)
Figure 3. Configuring the HTTP Invoke method action - The final action in this scenario is to send an email. The email needs to include data that is
returned from the HTTP endpoint to the voter who submitted the form. However, because the response
is returned as a string of JSON text, you need to parse this string into a JSON object. You can then
map the fields in the Gmail action. You can add a JSON parser
node to your flow to do this parsing.
- Click Add node (+), then from the Toolbox tab, click JSON parser.
- Click in the JSON Input field, click Insert a
mapping
, expand HTTP / Invoke method, and select Response body. This mapping indicates that you want to parse the response body that the previous HTTP GET action returns.
Figure 4. Configuring the JSON Input field, mapping the HTTP Invoke response body You can now use the Output schema fields to model the structure of the JSON schema. You need some sample JSON content from the response. You can get this content by calling the API from a browser by using a similar URL to the one that you specified in the HTTP invoke action. For example:https://API_URL?key=API_KEY&address=POSTCODE&electionId=ID_VALUE
- Expand the Output schema section and paste the sample response into the
Example JSON field. This field accepts a maximum of 16,000 characters, so you
might need to shorten the sample JSON content before you paste it in. For example, you might add
only one entry of an array or list.
Figure 5. Configuring the output schema - Click Generate schema to convert the sample JSON string into a JSON
object. You can see the result in the JSON schema field.
Figure 6. JSON schema generated from the example JSON
- Select Gmail as your final target application. Expand
Emails, then select Send email as the action. Map the
recipient's email address to the one that is entered in the Wufoo form. Specify a subject for the email, such as "Voter registration confirmation". Construct the
body of the email by using a combination of text strings and parsed fields that the JSON parser
generated.
Figure 7. An example of the completed Gmail fields
To test the flow, click Start flow, then click Dashboard to exit the flow. The Dashboard now includes a tile for your flow. You can use this tile to stop testing your flow, and to edit, export, or delete it.
![Flow icon on the Dashboard, showing the flow is running and awaiting an event Flow icon on the Dashboard, showing the flow is running and awaiting an event](../files/tut_jsonparser_dashboardtile_new.png)
Results
![Wufoo form submitted, as the New form entry event that triggers the flow Wufoo form submitted, as the New form entry event that triggers the flow](../files/jsonparserflow_wufooform_submitted01.png)
![Gmail message created by the flow Gmail message created by the flow](../files/jsonparserflow_gmail_email.png)