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:
moduleVariableis the variable corresponding to the module and set withrequire().resultis the object containing the analysis results.-
actionis 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.
-
jsPathConditionFlagis either a boolean or a string:true(boolean): the action specified by theactionparameter is performed only if the value ofjspathselects one or more nodes.false(boolean): the action specified by theactionparameter is performed only if the value ofjspathdoes not select any nodes.count(string): the action specified by theactionparameter is performed only if a certain amount of nodes are matched byjspath(see explanation below).multiple(string): the action specified by theactionparameter 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 integerwhere:
operatorcan be:- >
- <
- <=
- >=
- ==
- ===
integeris a non-negative integer number.
For example, if
jsPathConditionFlagis:count > 3it means that
jsPathmust identify at least three nodes for the action to be applied.multiple: In this context, the
multipleoption allows the users to implement advanced multiple validations within thejsPatharray by sequentially applying multiple JSONPaths, encompassing both absolute and relative paths. You can find an illustrative example in the in the modify paragraph. -
jsPathis either a single JSONPath expression or an array of JSONPath expressions that, in combination withjsPathAction(except when alsojsPathActionis a full JSONPath expression), determines the nodes to which the action must be applied. The elements of thejsPathparameters 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 theresultobject.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
-
jsPathActionis 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 byjspaththat matchesrelativepath.. relativepath: the action is applied to a child node selected byjspaththat matchesrelativepath.-
a JSONPath expression: the action is applied to the nodes selected by
jsPathActionifjspathConditionFlagis 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
valuesis 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(falsevalue), or to all the matched nodes (truevalue). 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(falsevalue), or to all the matched nodes (truevalue). 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, anindexproperty will be temporarily added to each extracted record during the jsonpath validation phase. This property will contain the position of each record within theextractionarray and will be automatically deleted after the jsonpath evaluation is applied. deleteJsPath(optional, defaulted tofalse) if set totrue, the items matched by thejsPathexpression will be removed from the output after the main action has been performed. Should the user activate this option whilejsPathActionis 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 thejsPathandjsPathexpressions will be printed in the console for debugging purposes. The user can also specify eitherjsPathorjsPathAction(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
recursiveparameter 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.