What is a JSON Action?
Insert excerpt |
---|
| _action_json |
---|
| _action_json |
---|
nopanel | true |
---|
|
actions process JSON data, and convert it into specific data objects that can be used by PhixFlow for processing.
The JSON action extracts one or more records from the provided JSON object using the JSON path specified. For the full list of JSON action Insert excerpt |
---|
| _property_settings |
---|
| _property_settings |
---|
nopanel | true |
---|
|
, see JSON Action Properties.
Creating JSON Actions
- Click and drag the
Insert excerpt |
---|
| _action_json |
---|
| _action_json |
---|
nopanel | true |
---|
|
icon from the toolbar onto the canvas - Enter a name for the JSON action, then select Create Action
- In the JSON action
Insert excerpt |
---|
| _property_settings |
---|
| _property_settings |
---|
nopanel | true |
---|
|
on the right, populate the following details:
Field | Description | Example Value |
---|
Name | Name given to the JSON Node. This will be displayed on the Actionflow canvas. | MyJSONReader |
Input Expression | This field is mandatory. The input expression provides the JSON data to be operated upon. Typically, this will be a simple expression pointing at an incoming attribute, such as, the body from a HTTP Node. | // Consists of the name of the connector and mapped attribute in.body
|
Use Strict JSON parsing | Defines the parsing of the JSON. Disabled is lenient and enabled is strict. Lenient parsing relaxes validation allowing the following to be present in the JSON data: - Use of single quotes, for example: {'name': 'Some “quotes” in a string'}
- Unquoted field names, for example: {name: “value”}
- Unescaped control characters, including literal new lines to appear within a string.
- Allow trailing commas {“name”: ”value”,}
| // Leaving the default option Insert excerpt |
---|
| _toggle_off |
---|
| _toggle_off |
---|
nopanel | true |
---|
|
|
Path | The JSON Path expression is evaluated against the data provided by the Input Expression and returns a list of JSON elements. It determines which elements are extracted from the JSON. The path starts at the root element represented by $ and each element in the path is separated by a full stop. The ^ traverses up a node and a . traverses down the node. Note that the path determines the number of nodes that are processed, which directly correlates to the number of records returned by the JSON action. For example, if the path returns the route root element of the JSON, only one record will be returned. Whereas a path that returns children nodes will return one record for each child element. | // JSON Path $.main_page.title
|
Tip |
---|
Use literal values or expressions encapsulated within ${} syntax, for example ${in.MyValue} . |
4.
Insert excerpt |
---|
| _finish |
---|
| _finish |
---|
nopanel | true |
---|
|
all settingsPath Syntax
Expression | Description |
---|
$
| symbol refers to the root element. |
@ | symbol refers to the current element. |
. | is the dot-child operator, which is used to denote a child element of the current element. |
[ ] | is used to select a child element of the current element (by name or index number). |
* | a wildcard, returning all elements regardless of their name. |
^ | symbol is used to traverse up 1 element in the JSON hierarchy from child to parent. |
Output Attributes
JSON is converted into data objects that can be used by PhixFlow, this information is accessed using the syntax, _result.
AttributeName.
HTML Comment |
---|
|
Node NodeWholePageOutputAttributes | nopanel | true |
---|
|
Worked Example
Here is a worked example using the Shop Data (available from the Learning Centre).
In this example, we are using:
- A Shop Staff screen containing a grid of the Shop Staff data and a blank tile - this screen was created using the Multi-Tile template
Tip |
---|
If you are completing this chapter as part of the Actionflow course and using a training instance, the data and screens have already been pre-loaded into the Actionflow Intermediate Application. For these example, we'll be working on the blank tile on the Shop Staff screen. |
GIF/SCREENSHOT OF FINISHED RESULT
Calculate Working Days
In this example, we'll use the bank holiday data created in 2.12 HTTP Action Configuration to calculate the remaining working days in the current month and map this onto a screen.
Table Setup
Create a new Insert excerpt |
---|
_tables | _tables | nopanel | true |
on your Insert excerpt |
---|
_erd | _erd | nopanel | true |
to save the working days data to, and ensure it contains the following attributes:- Name:
UID
- Type:
Integer
- Expression:
_NULL
- Name:
Date
- Type:
Date
- Expression:
_NULL
Event Handler Setup
On the Shop Staff screen, find the Working Days tile Insert excerpt |
---|
_lock | _lock | nopanel | true |
the screen to expand the tile Insert excerpt |
---|
_unlock | _unlock | nopanel | true |
the screen and click on the chevron (True/False Field) to open its Properties Create a new Actionflow on an Event Handler that will run when the chevron state is changed
On the Actionflow, set the Input as On ChangeWe will reuse the Actions created to retrieve the bank holiday dates.
Expand |
---|
|
If you have not completed the worked example in 2.12 HTTP Action Configuration, follow the steps to create the actions required for this chapter, excluding the Open Screen action.Image Removed |
Open the Actionflow that retrieves bank holidays from the government website, created in 2.12 HTTP Action ConfigurationHold down Ctrl and click on the Insert excerpt |
---|
_action_http | _action_http | nopanel | true |
action, the Insert excerpt |
---|
_action_json | _action_json | nopanel | true |
action and the Insert excerpt |
---|
_action_save | _action_save | nopanel | true |
actionRight-click and choose Insert excerpt |
---|
_extract_actionflow | _extract_actionflow | nopanel | true |
---|
- Name:
Get Gov Bank Holidays
Working Days Actionflow
Click back into the Calculate Working Days Insert excerpt |
---|
_actionflow | _actionflow | nopanel | true |
From the toolbar, click on Insert excerpt |
---|
_actionflow | _actionflow | nopanel | true |
to open the list of Actionflows in the repositoryDrag Get Gov Bank Holidays onto the canvas to create an embedded Insert excerpt |
---|
_actionflow | _actionflow | nopanel | true |
nodeConnect the Insert excerpt |
---|
_input | _input | nopanel | true |
to the embedded Actionflow nodeAdd a Insert excerpt |
---|
_action_calculate | _action_calculate | nopanel | true |
action to the canvas and connect it to the the embedded Actionflow nodeOn the Calculate action, add an Output Attribute that works out the last day of the current month:Name: LastDay
Type: String
Order: 1
Expression:
Code Block |
---|
|
do(
//set $lastDay to the 1st day of the month
$lastDay = dateSet(now(),_DAY,1),
//add a month to $lastDay
$lastDay = dateAdd($lastDay, _MONTH,1),
//minus a day to get the last day of the month
$lastDay = dateAdd($lastDay, _DAY,-1)
) |
Hover over the Calculate action to add a lookup Create a Insert excerpt |
---|
_action_view | _action_view | nopanel | true |
action that retrieves the bank holiday dataSet the Incoming and Outgoing Mappings:- Incoming:
LastDay
- Outgoing:
Date
Create a filter on the View that retrieves only bank holiday dates for England and Wales, and the current month
Expand |
---|
|
Image Removed |
On the Calculate action, add an Output Attribute that works out the working days:Name: CalculateWorkingDays
Type: Structured Data
Order: 2
Expression: Code Block |
---|
|
do(
//Get the last day of the month and set $lastDay to this
$lastDay = dateGet($lastDay,_DAY),
//for each day of the month, remove the time and set $allDays to now
forEach($day,1..$lastDay,
addElement($allDays, removeTime(dateSet(now(),_DAY,$day)
)
)
),
//remove BH days from month days
excluded($allDays, lu.Date)
) |
Hover over the Calculate action and select out, then create a Insert excerpt |
---|
_action_save | _action_save | nopanel | true |
action to map the data to the Working Days tableMap across the CalculateWorkingDays attribute from the Calculate action to the Date attribute on the Working Days tableTesting
- Test your Actionflow by clicking the chevron on the Shop Staff screen
1.02 Actionflow Principles HTML Comment |
---|
|
ZB I think the references to elements on this page should be replaced with references to "keys". JSON comes in key/value pairs in the syntax "key":"pair" - see https://www.w3schools.com/js/js_json_syntax.asp. Need to read up on this and make changes to this page. |
HTML Comment |
---|
|
Insert excerpt |
---|
| JSON Action Properties |
---|
| JSON Action Properties |
---|
name | WholePage |
---|
nopanel | true |
---|
|
|
Below is an example JSON object passed to the JSON action:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
firstline | 1 |
---|
title | Example JSON |
---|
linenumbers | true |
---|
|
// Example JSON string.
{
"main_page": {
"page": "PF Main Page",
"title": {
"name": "PF Title Text",
"data": [
{"initials": "AA", "value" : "Alistair Andrews"},
{"initials": "BB", "value" : "Bert Brown"}
]
}
}
} |
JSON Path: $.main_page.title
This path will bring back all elements matching the JSON path including the parent/grandparents and all child elements. The path defines the starting point from which we reference the JSON nodes.
The following examples show how to reference the returned JSON data:
_result
.name
, returns PF Title Text_result
.^.page
, returns PF Main Page- The ^ traverses up the JSON hierarchy
listToString(_result.data.initials)
, returns "AA, BB"
- Because there are multiple JSON nodes under
_result.data.initials
, an array of data is returned, see Array Handling Functions
Info |
---|
|
^ traverses up the hierarchy 1 layer.- . is the separator between nodes
- using .. traverses down the hierarchy 1 layer.
|
Retrieving Field Names
Code Block |
---|
title | Example JSON |
---|
linenumbers | true |
---|
|
{
"a": "1",
"b": 2,
"c": "xxx",
"d": {
"x": 1,
"y": 2,
"z": 3
}
} |
To retrieve the field names held in element d:
Code Block |
---|
do(
$json = jsonToItems(in.JsonIn),
$fields = getFields($json.d),
$fields[1]
) |
Returns the first field name of d which is “x” - see getFields.
Worked Example
Note |
---|
This example is taken from HTTP Action Configuration. If you have already completed this chapter as part of the Actionflow course, ensure you are familiar with the content before moving onto the next chapter. |
Insert excerpt |
---|
| HTTP Action Configuration |
---|
| HTTP Action Configuration |
---|
name | Example |
---|
nopanel | true |
---|
|
Image Added