jxpath()

Extracts values matching a JXPath expression from a Java object (map, array, etc.) and returns them.

Syntax

jxpath(SOURCE_EXPR, PATH_EXPR)

Parameters

SOURCE_EXPR
The target object or expression to traverse with JXPath. Supports Java objects such as maps, arrays, and lists.
PATH_EXPR
A JXPath expression string specifying the target to extract. Follows Apache Commons JXPath syntax.

Description

The jxpath() function uses the Apache Commons JXPath library to extract values from a Java object. Returns a list of all values matching PATH_EXPR. If no matching values are found, returns an empty list.

Unlike the xpath() function, which operates on XML strings, jxpath() operates on already-parsed Java objects (maps, arrays, lists).

Refer to the Apache Commons JXPath documentation for JXPath syntax.

Error codes

N/A

Usage examples

  1. Extracting the value of a specific key from a map

    json "{'data': {'name': 'alice', 'age': 30}}" | eval result = jxpath(data, "/name")
    | # result: ["alice"]
    
  2. Extracting a value from an array

    json "{'data': [1, 2, 3]}" | eval result = jxpath(data, "/.[1]")
    | # result: [1]
    
  3. NULL input

    json "{'data': null}" | eval result = jxpath(data, "/name")
    | # result: []
    

Compatibility

The jxpath() function has been available since before Sonar 4.0.