Implementation of the JMES Path JSON Query language for Java
—
Extensible function system with built-in implementations for common operations. Functions provide powerful data transformation and query capabilities within JMESPath expressions.
Abstract base class for all function implementations, providing the foundation for the extensible function system.
public abstract class JmesPathFunction implements JmesPathExpression {
/**
* Creates a function with the specified argument expressions
* @param expressions List of argument expressions
*/
public JmesPathFunction(List<JmesPathExpression> expressions);
/**
* Gets the list of argument expressions
* @return List of JmesPathExpression arguments
*/
public List<JmesPathExpression> getExpressions();
/**
* Evaluates the function with the provided evaluated arguments
* @param evaluatedArgs List of JsonNode values from evaluated arguments
* @return JsonNode result of the function evaluation
*/
public abstract JsonNode evaluate(List<JsonNode> evaluatedArgs);
public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input)
throws InvalidTypeException;
}Determines the length of arrays, objects, or strings. Essential for size-based filtering and validation.
public class JmesPathLengthFunction extends JmesPathFunction {
/**
* Creates a length function with variable arguments
* @param expressions Variable number of argument expressions
*/
public JmesPathLengthFunction(JmesPathExpression... expressions);
/**
* Creates a length function with a list of arguments
* @param arguments List of argument expressions
*/
public JmesPathLengthFunction(List<JmesPathExpression> arguments);
/**
* Evaluates the length of the provided argument
* @param evaluatedArgs List containing a single JsonNode argument
* @return IntNode containing the length value
* @throws InvalidTypeException If argument is not string, array, or object
*/
public JsonNode evaluate(List<JsonNode> evaluatedArgs) throws InvalidTypeException;
}Supported Types:
InvalidTypeExceptionUsage Examples:
// Get length of an array: length(items)
JmesPathLengthFunction arrayLength = new JmesPathLengthFunction(
new JmesPathField("items")
);
// Get length of a string field: length(name)
JmesPathLengthFunction stringLength = new JmesPathLengthFunction(
new JmesPathField("name")
);
// Use in filter: items[?length(tags) > 2]
JmesPathFilter lengthFilter = new JmesPathFilter(
new JmesPathField("items"),
new JmesPathIdentity(),
new OpGreaterThan(
new JmesPathLengthFunction(new JmesPathField("tags")),
new JmesPathLiteral(mapper.valueToTree(2))
)
);Checks if an array contains a specific value or if a string contains a substring.
public class JmesPathContainsFunction extends JmesPathFunction {
/**
* Creates a contains function with variable arguments
* @param expressions Variable number of argument expressions (subject, search)
*/
public JmesPathContainsFunction(JmesPathExpression... expressions);
/**
* Creates a contains function with a list of arguments
* @param expressions List of argument expressions (subject, search)
*/
public JmesPathContainsFunction(List<JmesPathExpression> expressions);
/**
* Evaluates whether the subject contains the search value
* @param evaluatedArgs List containing subject and search JsonNode values
* @return BooleanNode.TRUE if subject contains search, BooleanNode.FALSE otherwise
* @throws InvalidTypeException If subject is not string or array
*/
public JsonNode evaluate(List<JsonNode> evaluatedArgs);
}Behavior:
InvalidTypeExceptionUsage Examples:
// Check if array contains value: contains(categories, 'electronics')
JmesPathContainsFunction arrayContains = new JmesPathContainsFunction(
new JmesPathField("categories"),
new JmesPathLiteral("electronics")
);
// Check if string contains substring: contains(description, 'sale')
JmesPathContainsFunction stringContains = new JmesPathContainsFunction(
new JmesPathField("description"),
new JmesPathLiteral("sale")
);
// Use in filter: products[?contains(tags, 'featured')]
JmesPathFilter containsFilter = new JmesPathFilter(
new JmesPathField("products"),
new JmesPathIdentity(),
new JmesPathContainsFunction(
new JmesPathField("tags"),
new JmesPathLiteral("featured")
)
);Functions can be combined with other expressions for sophisticated queries:
// Complex query: items[?length(name) > 5 && contains(categories, 'tech')]
JmesPathFilter complexQuery = new JmesPathFilter(
new JmesPathField("items"),
new JmesPathIdentity(),
new JmesPathAndExpression(
new OpGreaterThan(
new JmesPathLengthFunction(new JmesPathField("name")),
new JmesPathLiteral(mapper.valueToTree(5))
),
new JmesPathContainsFunction(
new JmesPathField("categories"),
new JmesPathLiteral("tech")
)
)
);Functions can be used within projections and sub-expressions:
// Project with function: items[*].length(tags)
JmesPathProjection functionProjection = new JmesPathProjection(
new JmesPathField("items"),
new JmesPathLengthFunction(new JmesPathField("tags"))
);
// Nested function calls: length(items[?contains(name, 'pro')])
JmesPathLengthFunction nestedFunction = new JmesPathLengthFunction(
new JmesPathFilter(
new JmesPathField("items"),
new JmesPathIdentity(),
new JmesPathContainsFunction(
new JmesPathField("name"),
new JmesPathLiteral("pro")
)
)
);To implement custom functions, extend the JmesPathFunction base class:
public class CustomFunction extends JmesPathFunction {
public CustomFunction(List<JmesPathExpression> expressions) {
super(expressions);
}
@Override
public JsonNode evaluate(List<JsonNode> evaluatedArgs) {
// Custom function logic here
// Validate arguments and return JsonNode result
// Throw InvalidTypeException for type mismatches
}
}public class InvalidTypeException extends RuntimeException {
// Thrown when function arguments have incompatible types
}import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.IntNode;Functions work with Jackson's JsonNode system for type-safe JSON processing and return appropriate node types based on their operations.
Install with Tessl CLI
npx tessl i tessl/maven-com-amazonaws--jmespath-java