Skip to content

queryJsonPath

Use queryJsonPath to extract values from the JSONPath in a regex or an array format. This method is used in combination with the other actions to perform better validations according to the extracted data.

For example, consider this template:

TEMPLATE(CHARACTERS)
{
    @CHARACTER_NAME,
    @CHARACTER_NICKNAME,
    @CHARACTER_DATE_OF_BIRTH
}

If these rules:

SCOPE SENTENCE
{
    IDENTIFY(CHARACTERS) 
    {
        @CHARACTER_NAME[TYPE(NPH)]
        <>
        @CHARACTER_NICKNAME[KEYWORD("spider-man")]
        <>
        @CHARACTER_DATE_OF_BIRTH[TYPE(DAT)]
    }

    IDENTIFY(CHARACTERS)
    {
        @CHARACTER_NAME[TYPE(NPH)]
        <>
        @CHARACTER_NICKNAME[KEYWORD("peter parker")]
        <>
        @CHARACTER_DATE_OF_BIRTH[TYPE(DAT)]
    }
}

are applied to this input text:

Peter Parker, known as Spider-Man, was created by Stan Lee and Steve Ditko in 1962.

Miles Morales, known as the modern Peter Parker, was created by Brian Michael Bendis and Sara Pichelli in 2011.

you will get:

With this code:

function onFinalize(result) {
    var nickname_values = jsonPlug.queryJsonPath(result, "$..extraction[?(@.template == 'CHARACTERS')].fields[?(@.field == 'CHARACTER_NAME')].value", "regex", true);
    jsonPlug.jsonPlug(result, "delete", true, "$..extraction[?(@.template == 'CHARACTERS')].fields[?(@.field == 'CHARACTER_NICKNAME' && " + nickname_values + ".test(@.value) )]", "#this#", true);
    return result;
}

you will get:

As you can see, the nickname_values variable has been populated with all the values of the CHARACTER_NAME field of the CHARACTERS template from the JSONPath.

The delete action of the jsonPlug method below the variable definition allows you to delete the CHARACTER_NICKNAME field if its value is matched by the regular expression generated in the variable.

With this other example:

function onFinalize(result) {
    var nickname_values = jsonPlug.queryJsonPath(result, "$..extraction[?(@.template == 'CHARACTERS')].fields[?(@.field == 'CHARACTER_NAME')].value", "regex", true);
    var character_has_conflicting_nickname = jsonPlug.queryJsonPath(result,"$..extraction[?(@.template == 'CHARACTERS')].fields[?(@.field == 'CHARACTER_NICKNAME' && " + nickname_values + ".test(@.value) )]", "boolean");
    CONSOLE.log(character_has_conflicting_nickname);
}

you will get the boolean value of true as output in the Output tab of the Console tool window, because the value of the CHARACTER_NICKNAME field of the second sentence is equal to the CHARACTER_NAME value of the first sentence. In case of no match, a value of false would have been reported.

The syntax of the queryJsonPath method is:

moduleVariable.queryJsonPath(result, jsPath, outputType, modifierFlag)

where:

  • moduleVariable is the variable corresponding to the module and set with require().
  • result is the object containing the analysis results.
  • jsPath is the JSONPath expression that determines the nodes to which the action must be applied. It can be:

    • A standard JSONPath.

    Or:

    • An array of multiple JSONPaths (see modify).
  • outputType is the output type, it can be:

    • array: it contains the matched value(s) by the JSONPath query.
    • regex: like array, but the string/digit values are turned into regular expression format.
    • paths: it contains advanced query information for further script manipulations.
    • boolean: boolean value of true in case of a match, false otherwise.
  • modifierFlag is a flag:

    • If you select regex as outputType and the flag is set to true, it will make the regular expression case insensitive.
    • If you select array as outputType and the flag is set to false, it will stop the deletion of duplicates.

Alternatively, you can use this syntax:

moduleVariable.queryJsonPath(result, {
    jsPath: parameterValue,
    outputType: parameterValue,
    modifierFlag: parameterValue
})

where parameterValue is one of the possible values of the corresponding parameters described above.

Note

  • The parameters in the second syntax can be declared in any order.
  • Both syntaxes can be used interchangeably.