Extract data with a regex
In this example, you will create an Xtension A set of instructions for Nintex Automation Cloud to use third-party API functionality with Nintex workflows. An Xtension may include workflow actions, start events, forms or file control. that asks a question of WolframAlpha, and use it to calculate the opportunity cost of business development proposals. |
A proposal's opportunity cost is the value that would have been received had the proposal's expenditure not been spent: for example, by keeping the money in a high interest account for the same amount of time. If a proposal's expected return can't outperform its opportunity cost, it may be unsuited to the business's goals.
Calculating the future value of an investment accurately is a complex equation. In this example, you will:
- Use WolframAlpha to calculate the result for you.
- Extract the value from the response using the QueryJSON action.
- Use an Apply regular expression action to convert the value into a number for comparison.
The complete OpenAPI Specification A standard, language-agnostic description of RESTful APIs that can be read by both humans and machines. Formerly known as Swagger. and icon for this example are available here.
The WolframAlpha API
WolframAlpha interprets the language of a question and calculates answers based on its own store of data and algorithms. You can ask questions based on facts, figures and calculations, such as the distance between two cities, how many planes are currently above you, or how much a thousand dollars will be worth in a hundred years.
For more examples of questions you can ask, see https://www.wolframalpha.com/examples/
Create the Xtension
Step 1: Register for the WolframAlpha API key
- Sign in or create a new a developer account with WolframAlpha at developer.wolframalpha.com/portal/signin/html.
- Click on the My Apps tab.
- Click Get an AppID.
- Sign up for an AppID.
- Type in a name and description for your app.
- Record the App id when it is displayed: you will need it when creating a connection for your Xtension in Nintex Automation Cloud.
Step 2: Build the OpenAPI Specification
Build an OpenAPI Specification that:
- Uses http with the host The domain name of the third-party API's URL. of api.wolframalpha.com and a base path of /v2.
- Passes the API key authentication An authorization code passed in an API request, either inside the header or as a parameter, to identify the requester. in the query Part of the URL that does not fit into the path structure, and provides additional information or parameters for the request. The query is prefaced by a question mark (?) in the URL, for example: http://example.com?color=blue. with the name appid.
- Uses the GET method on the path The part of the URL after the hostname that directs the request to a specific resources within the host. For example, the section after "example.com" in http://example.com/this/is/a/path. /query with
three required parameters A piece of information passed to a third-party API during a request. in the
query Part of the URL that does not fit into the path structure, and provides additional information or parameters for the request. The query is prefaced by a question mark (?) in the URL, for example: http://example.com?color=blue.:
- input: the question to ask.
- output: the format to return the output as. Create an enum with the single value of json to ensure the workflow designer selects the correct format.
- includepodid: the section of the results to return. Create an enum with the single value of Result to ensure the workflow designer selects the correct results.
- Accepts a results object.
For information on defining enums or multiple parameters, see Define the parameters
For more information on using x-ntx-summary, see Rename fields (x-ntx-summary).
{
"swagger": "2.0",
"info": {
"version": "1.0",
"title": "WolframAlpha",
"description": "This example sends a question to WolframAlpha"
},
"host": "api.wolframalpha.com",
"basePath": "/v2",
"schemes": [ "https" ],
"consumes": [ "application/json" ],
"produces": [ "application/json" ],
"paths": {
"/query": {
"get": {
"summary": "WolframAlpha query",
"description": "Answer a question",
"operationId": "WolframAlpha",
"produces": [ "application/json" ],
"security": [
{
"myApiKey": []
}
],
"parameters": [
{
"name": "input",
"in": "query",
"type": "string",
"required": true,
"description": "Type your question"
},
{
"name": "output",
"in": "query",
"type": "string",
"required": true,
"x-ntx-summary": "Select JSON format",
"enum": [ "json" ]
},
{
"name": "includepodid",
"in": "query",
"type": "string",
"required": true,
"x-ntx-summary": "Select Result",
"enum": [ "Result" ]
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object"
}
}
}
}
}
},
"securityDefinitions": {
"myApiKey": {
"type": "apiKey",
"name": "appid",
"in": "query"
}
}
}
Construct the query
Step 1: Examine the JSON response
You can preview the response The return from a third-party API after a request has been made by the client. by copying the following into your browser and adding your API key:
http://api.wolframalpha.com/v2/query?input=continuous+interest+ PV%3D%243800%2C+rate%3D6%25%2C+periods%3D5&output= json&includepodid=Result&appid=YOUR_APP_KEY_HERE
The API returns a complex JSON object.
Step 2: Test your query
Create a JSON query to navigate to the plaintext key in the first subpods element:
queryresult.pods[0].subpods[0].plaintext
You can test your JSON JavaScript Object Notation: a data format based on JavaScript that is commonly used for API request parameters and responses. query using the Nintex Automation Cloud tools.
- Navigate to https://jsontostring.com/.
- Paste your sample JSON in the converter and click Convert.
- In another tab, navigate to Nintex Platform tools.
- In the query body template on the right-hand side of the page:
- Replace the "string" value next to "JSON" with the JSON string you converted in step 2.
- Replace the "string" value next to "JSONpath" with the JSON expression you want to test.
- Click Send API Request.
- Check the returned response.
{
"queryresult": {
"success": true,
"error": false,
"numpods": 1,
"datatypes": "Formula",
"timedout": "",
"timedoutpods": "",
"timing": 1.964,
"parsetiming": 1.2650000000000001,
"parsetimedout": false,
"recalculate": "",
"id": "MSPa5961h4he2i1hh195db700004gg3d863ig3dchg5",
"host": "http://www3.wolframalpha.com",
"server": "7",
"related": "http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?
id=MSPa5971h4he2i1hh195db700006027ad7hdbc1ai61&s=7",
"version": "2.6",
"pods": [
{
"title": "Result",
"scanner": "Formula",
"id": "Result",
"position": 100,
"error": false,
"numsubpods": 1,
"primary": true,
"subpods": [
{
"title": "",
"img": {
"src": "http://www3.wolframalpha.com/Calculate/MSP/
MSP5981h4he2i1hh195db700003fh1732cgici61d8?
MSPStoreType=image/gif&s=7",
"alt": "future value | Au$5129.46 (Australian dollars)",
"title": "future value | Au$5129.46 (Australian dollars)",
"width": 333,
"height": 36
},
"plaintext": "future value | Au$5129.46 (Australian dollars)"
}
],
"states": [
{
"name": "Step-by-step solution",
"input": "Result__Step-by-step solution"
}
]
}
],
"assumptions": {
"type": "FormulaSelect",
"template": "Assuming ${desc1}. Use ${desc2} instead",
"count": 2,
"values": [
{
"name": "PresentValueFutureValueContinuous",
"desc": "present and future value (continuous compounding)",
"input": "FSelect_**PresentValueFutureValueContinuous--"
},
{
"name": "AnnuityFutureValueLumpsum",
"desc": "annuity future value",
"input": "FSelect_**AnnuityFutureValueLumpsum--"
}
]
}
}
}
Construct the regular expression
The plaintext from WolframAlpha is a string that includes some description with the final numerical value. To extract the value from the string, create a regular expression to match against positive or negative numbers with up to two figures after the decimal point: -?(0|([1-9]\d*))(\.\d+)?
Tip: You can test your regular expressions at https://regex101.com/
For more information on regular expressions, see http://www.regular-expressions.info/quickstart.html
Build the workflow
Step 1: Add your Xtension
Import the OpenAPI Specification you created into Nintex Automation Cloud:
- Open your Nintex Automation Cloud tenancy.
- Click Xtensions in the dashboard to open the Xtensions page.
- Click in the Private connector list.
- Click Choose a file. Navigate to the OpenAPI Specification on your computer.
- Wait for Nintex Automation Cloud to validate the file.
- Click Next.
- Nintex Automation Cloud detects the API key security
template.
- Click Next.
- Edit the Name of the Xtension, which becomes the name of the action group in the Workflow designer.
- Edit the Description of the Xtension. This appears in the Private connector list in the Xtensions page.
- Select or upload an icon for the Xtension. This is displayed for each action or event in the Workflow designer.
- Click Publish.
Step 2: Create the workflow
For more information on creating workflows, see the Workflow Designer.
- Click Create workflow in your Nintex Automation Cloud tenancy.
- Configure the Start event to be a Nintex form with six variables:
- The name of the person submitting the proposal (text).
- The email address of the person submitting the proposal (text).
- The description of the proposal (text).
- The cost of the proposal (decimal).
- The expected return on investment (decimal).
- The number of months until that return is reached (integer).
For more information on designing forms in Nintex Automation Cloud, see Design a form.
- Drag a Set a variable value action after the Start event, and create a
variable to store your interest rate.
For more information, see Set a variable value.Tip: Storing your interest rate in a variable makes it easier to update the rate later.
- Drag a WolframAlpha query action after the Set a variable value action.
- Create a connection to the WolframAlpha API using your API key.
- Configure the WolframAlpha query action:
- Type continuous interest PV=$[COST], rate=[RATE]%, periods=[MONTHS], using your public web form cost and months variables and interest rate variable.
- Select JSON in the Select JSON format field.
- Select Result in the Select Result field.
- Create a collection variable to store the answer from WolframAlpha.
- Drag a Query JSON action after the WolframAlpha query action, and configure it to:
- Query the WolframAlpha results variable.
- Use the JSON query you created earlier.
- Store the results in a new text variable.
For more information, see Query JSON. - Drag an Apply a regular expression action after the Query JSON action and configure it to:
- Use the Query JSON result variable.
- Use the Extract operation.
- Use the regular expression pattern you defined earlier.
- Ignore case.
- Store the first result in a new decimal variable.
For more information, see Apply a regular expression. - Drag a Calculate a value action after the Apply a regular expression action
and configure it to:
- Multiply the decimal result value from the regular expression by 1.
- Store the result back in the same variable.
For more information, see Calculate a value.
- Drag a Branch by condition action after the Calculate a value action.
For more information, see Branch by condition. - Configure the Branch by condition action to test whether the regular expression result variable is less than
the expected return on investment variable.
Configure the two branches: - In the No branch, drag a Send an email action and configure it to send an email informing the person who submitted the proposal that it has not been accepted.
- In the Yes branch, drag a Send an email
action and configure it to send an email to the business managers with the proposal details, and carbon
copy the proposal submitter.
- Click Test to test the workflow.
- Save or publish the workflow.
Tip: If you want to troubleshoot an Xtension, select Development as the Assigned Use when you publish the workflow. Development workflows display more detailed error messages in their instance details. Republish your workflow as Production when you're ready to use it.