jmespath

Performs a JMESPath search on an input JSON format, when using a transformer.

python · Filters And Transformers

Details

IDjmespath
Languagepython
From Version5.0.0
Docker Imagedemisto/py3-tools:1.0.0.11597437
Tagstransformer general

README

Performs a JMESPath search on an input JSON format, when using a transformer.

Script Data


Name Description
Script Type python3
Tags transformer, general
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
expression JMESPath expression
value Original input value

Outputs


There are no outputs for this script.

import demistomock as demisto
import jmespath
from CommonServerPython import *


def jmespath_search(expression: str, value: dict | list) -> dict:
    try:
        expression_compiled = jmespath.compile(expression)  # pylint: disable=E1101
    except Exception as err:
        raise Exception(f"Invalid expression - {err}")
    result = expression_compiled.search(value)
    return result


def main():
    args = demisto.args()
    value = args.get("value")
    if isinstance(value, str):
        try:
            value = json.loads(value)
        except Exception as err:
            return_error(f"The input is not valid JSON: {err}")
    expression = args.get("expression")
    result = jmespath_search(expression, value)
    return_results(result)


if __name__ in ["__builtin__", "builtins"]:
    main()