Syntax and actions
Syntax
The syntax of the jsonPlug
method is:
moduleVariable.jsonPlug(result, {
action: parameterValue,
jsPathConditionFlag: parameterValue,
jsPath: parameterValue,
jsPathAction: parameterValue,
values: parameterValue [,
recursive: parameterValue,
recursiveAction: parameterValue,
skipNameValidation: parameterValue,
addIndexToRecords: parameterValue,
deleteJsPath: parameterValue,
printMatchedItems: parameterValue]
});
Note
The parts in square brackets are optional.
parameterValue
is one of the possible values of the corresponding parameters described below:
moduleVariable
is the variable corresponding to the module and set withrequire()
.result
is the object containing the analysis results.-
action
is the action to perform on the results object. The available values for this parameter are:- delete
- delete template
- delete record
- add field
- add fields
- add template
- add record
- add property
- add instance
- add instances
- add key
- clone
- clone new
- clone new multiple
- clone value
- clone template
- clone record
- clone instances
- add category
- modify
- modify regex
- apply math
Note
The following actions can be used interchangeably, producing the same output:
- delete template and delete record.
- clone template and clone record.
- add field and add fields.
- add template and add record.
- add instance and add instances.
- add property and add key.
-
jsPathConditionFlag
is either a boolean or a string:true
(boolean): the action specified by theaction
parameter is performed only if the value ofjspath
selects one or more nodes.false
(boolean): the action specified by theaction
parameter is performed only if the value ofjspath
does not select any nodes.count
(string): the action specified by theaction
parameter is performed only if a certain amount of nodes are matched byjspath
(see explanation below).multiple
(string): the action specified by theaction
parameter is performed only if multiple absolute and relative nodes validations are verified byjspath
(see explanation below).
count: In this scenario, the module is designed to verify the number of nodes, referred to as — the count — selected by
jspath
. The action is executed exclusively when the specified condition is met (as detailed below). The string follows this format:count operator integer
where:
operator
can be:- >
- <
- <=
- >=
- ==
- ===
integer
is a non-negative integer number.
For example, if
jsPathConditionFlag
is:count > 3
it means that
jsPath
must identify at least three nodes for the action to be applied.multiple: In this context, the
multiple
option allows the users to implement advanced multiple validations within thejsPath
array by sequentially applying multiple JSONPaths, encompassing both absolute and relative paths. You can find an illustrative example in the in the modify paragraph. -
jsPath
is either a single JSONPath expression or an array of JSONPath expressions that, in combination withjsPathAction
(except when alsojsPathAction
is a full JSONPath expression), determines the nodes to which the action must be applied. The elements of thejsPath
parameters are evaluated in couples:- A boolean that is a flag necessary for the application of the action. If
true
, the action is performed to the nodes selected by the JSONPath expression. - A JSONPath expression indicating the nodes where the action must be performed.
Info
Several useful resources are available on the Web to get more information about JSONPath syntax and to test JSONPath expressions.
Warning
It is suggested to avoid using the recursive
..
iterator inside a JSONPath expression, as it would process unncessary nodes within theresult
object.For example, if you need to match every category, it is preferable to use
$.match_info.rules.categorization[0:]
instead of$..categorization
.The latter will process every child node starting from the root object, potentially causing unnecessary processing overhead.
By specifying the exact path in the JSONPath expression, you can optimize the matching process and improve the efficiency of your scripting.
- A boolean that is a flag necessary for the application of the action. If
-
jsPathAction
is either a special value, a single JSONPath expression or an array of JSONPath expressions (for the array case, see the second example in clone) that, alone or in combination withjsPath
, determines where the action must be applied. Its value can be:#this#
or.
: the action is applied to the same nodes selected byjspath
.^
: the action is applied to the parent nodes of the nodes selected byjspath
.^ relativepath
: the action is applied to the sibling nodes of the nodes selected byjspath
that matchesrelativepath
.. relativepath
: the action is applied to a child node selected byjspath
that matchesrelativepath
.-
a JSONPath expression: the action is applied to the nodes selected by
jsPathAction
ifjspathConditionFlag
is satisfied. In case of an array, the elements of the expression are:- A boolean that is a flag necessary for the application of the action. If
true
, the action is performed to the nodes selected by the JSONPath expression. - A JSONPath expression indicating the nodes where the action must be performed.
- A boolean that is a flag necessary for the application of the action. If
Note
Even though the jsPathAction
parameter is usually mandatory, it can be omitted (or passed empty) when one of the following actions is used:
add category
, add template
and add record
, clone template
and clone record
, clone new
, clone new multiple
values
is an array containing the specification of the action. The meaning of the array items and their order vary according to the action; a few actions do not use it.
Note
When a single string is expected in the array (e.g. in the modify
or clone record
actions, values
can also be directly declared as a single string. This simplifies the syntax and makes it more concise.
recursive
(optional) is a boolean that determines whether the action should be applied to only the first node selected byjsPath
(false
value), or to all the matched nodes (true
value). If not explicitly declared, it defaults totrue
.recursiveAction
(optional) is a boolean that determines whether the action should be applied to only the first node selected byjsPathAction
(false
value), or to all the matched nodes (true
value). If not explicitly declared, it defaults totrue
.-
skipNameValidation
(optional, defaulted tofalse
) if set totrue
, the template and field validation will be skipped. This optional flag is used for:- modify
- modify regex
- add field and add fields
- add template and add record
- clone
- clone template and clone record
- clone new
- clone new multiple
- clone value
- clone instances
-
addIndexToRecords
(optional, defaulted tofalse
) if set totrue
, anindex
property will be temporarily added to each extracted record during the jsonpath validation phase. This property will contain the position of each record within theextraction
array and will be automatically deleted after the jsonpath evaluation is applied. deleteJsPath
(optional, defaulted tofalse
) if set totrue
, the items matched by thejsPath
expression will be removed from the output after the main action has been performed. Should the user activate this option whilejsPathAction
is either equal or directly refers tojsPath
, an exception will be raised.printMatchedItems
(optional, defaulted tofalse
) if set totrue
(boolean) orboth
(string), the items matched by thejsPath
andjsPath
expressions will be printed in the console for debugging purposes. The user can also specify eitherjsPath
orjsPathAction
(string) to print only the items matched by that specific JSONPath expressions.
Alternatively, you can use this syntax:
moduleVariable.jsonPlug(result, action, jsPathConditionFlag, jsPath, jsPathAction, recursive, values, skipNameValidation, addIndexToRecords, recursiveAction, deleteJsPath, printMatchedItems)
Note
- The parameters in the second syntax must be declared in this exact order.
- In this case, the
recursive
parameter MUST be declared. - Both syntaxes can be used interchangeably.
Actions
The following pages will describe the possible values of the action
parameter. The value of the values
parameter is interpreted based upon the value of action
.
Note
All actions changing the categorization results will automatically re-calculate the frequency score of each category.
The example codes will be written according to the first syntax explained above.