Implementation of the JMES Path JSON Query language for Java
—
Comprehensive set of comparison operators for filtering and conditional operations in JMESPath expressions. Includes equality, inequality, and numeric comparisons with proper type handling.
Abstract base class for all comparison operators, providing the fundamental structure and interface.
public abstract class Comparator implements JmesPathExpression {
/**
* Creates a comparator with left and right expressions
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public Comparator(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Gets the left-hand side expression
* @return The LHS expression
*/
public JmesPathExpression getLhsExpr();
/**
* Gets the right-hand side expression
* @return The RHS expression
*/
public JmesPathExpression getRhsExpr();
/**
* Determines if the left and right values match according to this comparator
* @param lhs Left-hand side JsonNode value
* @param rhs Right-hand side JsonNode value
* @return true if the comparison succeeds, false otherwise
*/
public abstract boolean matches(JsonNode lhs, JsonNode rhs);
public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input)
throws InvalidTypeException;
}Tests for equality between two JSON values using deep comparison semantics.
public class OpEquals extends Comparator {
/**
* Creates an equality comparison operator
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public OpEquals(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Tests if two JsonNode values are equal
* @param lhs Left-hand side value
* @param rhs Right-hand side value
* @return true if values are equal, false otherwise
*/
public boolean matches(JsonNode lhs, JsonNode rhs);
}Usage Example:
// Create equality comparison: field == "value"
OpEquals equalityCheck = new OpEquals(
new JmesPathField("status"),
new JmesPathLiteral("active")
);Tests for inequality between two JSON values.
public class OpNotEquals extends Comparator {
/**
* Creates an inequality comparison operator
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public OpNotEquals(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Tests if two JsonNode values are not equal
* @param lhs Left-hand side value
* @param rhs Right-hand side value
* @return true if values are not equal, false otherwise
*/
public boolean matches(JsonNode lhs, JsonNode rhs);
}Usage Example:
// Create inequality comparison: field != "value"
OpNotEquals inequalityCheck = new OpNotEquals(
new JmesPathField("type"),
new JmesPathLiteral("deprecated")
);Abstract base class for numeric comparisons that handles type conversion and validation.
public abstract class NumericComparator extends Comparator {
/**
* Creates a numeric comparator with left and right expressions
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public NumericComparator(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Compares JsonNode values as numbers, handling type conversion
* @param lhs Left-hand side JsonNode value
* @param rhs Right-hand side JsonNode value
* @return true if numeric comparison succeeds, false otherwise
*/
public final boolean matches(JsonNode lhs, JsonNode rhs);
/**
* Performs the actual numeric comparison on BigDecimal values
* @param lhs Left-hand side BigDecimal value
* @param rhs Right-hand side BigDecimal value
* @return true if comparison succeeds, false otherwise
*/
public abstract boolean matches(BigDecimal lhs, BigDecimal rhs);
}Compares two numeric values for less-than relationship.
public class OpLessThan extends NumericComparator {
/**
* Creates a less-than comparison operator
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public OpLessThan(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Tests if left value is less than right value
* @param lhs Left-hand side BigDecimal value
* @param rhs Right-hand side BigDecimal value
* @return true if lhs < rhs, false otherwise
*/
public boolean matches(BigDecimal lhs, BigDecimal rhs);
}Usage Example:
// Create less-than comparison: age < 30
OpLessThan lessThanCheck = new OpLessThan(
new JmesPathField("age"),
new JmesPathLiteral(mapper.valueToTree(30))
);Compares two numeric values for less-than-or-equal relationship.
public class OpLessThanOrEqualTo extends NumericComparator {
/**
* Creates a less-than-or-equal comparison operator
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public OpLessThanOrEqualTo(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Tests if left value is less than or equal to right value
* @param lhs Left-hand side BigDecimal value
* @param rhs Right-hand side BigDecimal value
* @return true if lhs <= rhs, false otherwise
*/
public boolean matches(BigDecimal lhs, BigDecimal rhs);
}Compares two numeric values for greater-than relationship.
public class OpGreaterThan extends NumericComparator {
/**
* Creates a greater-than comparison operator
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public OpGreaterThan(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Tests if left value is greater than right value
* @param lhs Left-hand side BigDecimal value
* @param rhs Right-hand side BigDecimal value
* @return true if lhs > rhs, false otherwise
*/
public boolean matches(BigDecimal lhs, BigDecimal rhs);
}Compares two numeric values for greater-than-or-equal relationship.
public class OpGreaterThanOrEqualTo extends NumericComparator {
/**
* Creates a greater-than-or-equal comparison operator
* @param lhsExpr Left-hand side expression
* @param rhsExpr Right-hand side expression
*/
public OpGreaterThanOrEqualTo(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
/**
* Tests if left value is greater than or equal to right value
* @param lhs Left-hand side BigDecimal value
* @param rhs Right-hand side BigDecimal value
* @return true if lhs >= rhs, false otherwise
*/
public boolean matches(BigDecimal lhs, BigDecimal rhs);
}// Filter array: users[?age > 18]
JmesPathFilter adultFilter = new JmesPathFilter(
new JmesPathField("users"), // Array to filter
new JmesPathIdentity(), // Project identity (whole element)
new OpGreaterThan( // Comparison condition
new JmesPathField("age"),
new JmesPathLiteral(mapper.valueToTree(18))
)
);// Multiple conditions: items[?price < 100 && category != "deprecated"]
JmesPathFilter complexFilter = new JmesPathFilter(
new JmesPathField("items"),
new JmesPathIdentity(),
new JmesPathAndExpression( // AND logical operation
new OpLessThan(
new JmesPathField("price"),
new JmesPathLiteral(mapper.valueToTree(100))
),
new OpNotEquals(
new JmesPathField("category"),
new JmesPathLiteral("deprecated")
)
)
);import java.math.BigDecimal;
import com.fasterxml.jackson.databind.JsonNode;The numeric comparators use BigDecimal for precise numeric comparisons and support various JSON numeric types including integers and floating-point numbers.
Install with Tessl CLI
npx tessl i tessl/maven-com-amazonaws--jmespath-java