CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-amazonaws--jmespath-java

Implementation of the JMES Path JSON Query language for Java

Pending
Overview
Eval results
Files

advanced-expressions.mddocs/

Advanced Expressions

Complex expression types for sophisticated JSON querying including projections, filters, logical operations, and array manipulations. These expressions enable powerful data transformation and conditional processing.

Capabilities

List Projection

Projects an expression over each element of an array, creating a new array with the results.

public class JmesPathProjection implements JmesPathExpression {
    /**
     * Creates a list projection expression
     * @param lhsExpr Expression that evaluates to an array
     * @param projectionExpr Expression to apply to each array element
     */
    public JmesPathProjection(JmesPathExpression lhsExpr, JmesPathExpression projectionExpr);
    
    /**
     * Gets the left-hand side expression (array source)
     * @return The LHS expression
     */
    public JmesPathExpression getLhsExpr();
    
    /**
     * Gets the projection expression (applied to each element)
     * @return The projection expression
     */
    public JmesPathExpression getProjectionExpr();
    
    public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input) 
        throws InvalidTypeException;
}

Usage Examples:

// Project names from users array: users[*].name
JmesPathProjection nameProjection = new JmesPathProjection(
    new JmesPathField("users"),
    new JmesPathField("name")
);

// Project complex sub-expression: items[*].details.price
JmesPathProjection complexProjection = new JmesPathProjection(
    new JmesPathField("items"),
    new JmesPathSubExpression(
        new JmesPathField("details"),
        new JmesPathField("price")
    )
);

Value Projection

Projects an expression over the values of an object, creating an array from object values.

public class JmesPathValueProjection implements JmesPathExpression {
    /**
     * Creates a value projection expression
     * @param lhsExpr Expression that evaluates to an object
     * @param rhsExpr Expression to apply to each object value
     */
    public JmesPathValueProjection(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr);
    
    /**
     * Gets the left-hand side expression (object source)
     * @return The LHS expression
     */
    public JmesPathExpression getLhsExpr();
    
    /**
     * Gets the right-hand side expression (applied to each value)
     * @return The RHS expression
     */
    public JmesPathExpression getRhsExpr();
    
    public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input) 
        throws InvalidTypeException;
}

Usage Example:

// Project over object values: config.*.enabled
JmesPathValueProjection valueProjection = new JmesPathValueProjection(
    new JmesPathField("config"),
    new JmesPathField("enabled")
);

Filter Expression

Filters array elements based on a comparison condition, returning elements that match the criteria.

public class JmesPathFilter implements JmesPathExpression {
    /**
     * Creates a filter expression
     * @param lhsExpr Expression that evaluates to an array
     * @param rhsExpr Expression to apply to matching elements
     * @param comparator Comparison expression for filtering
     */
    public JmesPathFilter(JmesPathExpression lhsExpr, JmesPathExpression rhsExpr, 
                         JmesPathExpression comparator);
    
    /**
     * Gets the right-hand side expression (applied to matching elements)
     * @return The RHS expression
     */
    public JmesPathExpression getRhsExpr();
    
    /**
     * Gets the left-hand side expression (array source)
     * @return The LHS expression
     */
    public JmesPathExpression getLhsExpr();
    
    /**
     * Gets the comparator expression (filter condition)
     * @return The comparator expression
     */
    public JmesPathExpression getComparator();
    
    public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input) 
        throws InvalidTypeException;
}

Usage Examples:

// Filter active users: users[?active == true]
JmesPathFilter activeUsers = new JmesPathFilter(
    new JmesPathField("users"),
    new JmesPathIdentity(),
    new OpEquals(
        new JmesPathField("active"),
        new JmesPathLiteral(mapper.valueToTree(true))
    )
);

// Filter with projection: products[?price > 100].name
JmesPathFilter expensiveProductNames = new JmesPathFilter(
    new JmesPathField("products"),
    new JmesPathField("name"),
    new OpGreaterThan(
        new JmesPathField("price"),
        new JmesPathLiteral(mapper.valueToTree(100))
    )
);

Array Flattening

Flattens nested arrays by one level, combining array elements into a single array.

public class JmesPathFlatten implements JmesPathExpression {
    /**
     * Creates a flatten expression
     * @param toFlatten Expression that evaluates to an array
     */
    public JmesPathFlatten(JmesPathExpression toFlatten);
    
    /**
     * Gets the expression to flatten
     * @return The flatten expression
     */
    public JmesPathExpression getFlattenExpr();
    
    public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input) 
        throws InvalidTypeException;
}

Usage Example:

// Flatten nested arrays: categories[]
JmesPathFlatten flattenCategories = new JmesPathFlatten(
    new JmesPathField("categories")
);

Flattening Behavior:

  • Non-array elements are added directly to the result
  • Array elements have their contents added individually
  • Only flattens one level deep
  • Returns NullNode if input is not an array

Logical AND Expression

Performs logical AND operation with short-circuit evaluation semantics.

public class JmesPathAndExpression implements JmesPathExpression {
    /**
     * Creates a logical AND expression
     * @param lhsExpr Left-hand side expression
     * @param rhsExpr Right-hand side expression
     */
    public JmesPathAndExpression(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();
    
    public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input) 
        throws InvalidTypeException;
}

Evaluation Logic:

  • If LHS evaluates to BooleanNode.TRUE, returns RHS result
  • Otherwise, returns LHS result (short-circuit evaluation)

Logical NOT Expression

Performs logical negation, inverting truth-like values.

public class JmesPathNotExpression implements JmesPathExpression {
    /**
     * Creates a logical NOT expression
     * @param expr Expression to negate
     */
    public JmesPathNotExpression(JmesPathExpression expr);
    
    /**
     * Gets the expression to negate
     * @return The negated expression
     */
    public JmesPathExpression getExpr();
    
    public <Input, Output> Output accept(JmesPathVisitor<Input, Output> visitor, Input input) 
        throws InvalidTypeException;
}

Evaluation Logic:

  • Returns BooleanNode.TRUE if operand is not BooleanNode.TRUE
  • Returns BooleanNode.FALSE if operand is BooleanNode.TRUE

Complex Usage Patterns

Combined Projections and Filters

// Complex query: items[?price > 100][*].details.name
JmesPathProjection complexQuery = new JmesPathProjection(
    new JmesPathFilter(
        new JmesPathField("items"),
        new JmesPathIdentity(),
        new OpGreaterThan(
            new JmesPathField("price"),
            new JmesPathLiteral(mapper.valueToTree(100))
        )
    ),
    new JmesPathSubExpression(
        new JmesPathField("details"),
        new JmesPathField("name")
    )
);

Nested Logical Operations

// Multi-condition filter: users[?active && !suspended && age >= 18]
JmesPathFilter complexFilter = new JmesPathFilter(
    new JmesPathField("users"),
    new JmesPathIdentity(),
    new JmesPathAndExpression(
        new JmesPathAndExpression(
            new JmesPathField("active"),
            new JmesPathNotExpression(new JmesPathField("suspended"))
        ),
        new OpGreaterThanOrEqualTo(
            new JmesPathField("age"),
            new JmesPathLiteral(mapper.valueToTree(18))
        )
    )
);

Value Projection with Processing

// Process object values: settings.*.value.enabled
JmesPathValueProjection processedValues = new JmesPathValueProjection(
    new JmesPathField("settings"),
    new JmesPathSubExpression(
        new JmesPathField("value"),
        new JmesPathField("enabled")
    )
);

Flattening with Projection

// Flatten and project: groups[].members[*].name
JmesPathProjection flattenedProjection = new JmesPathProjection(
    new JmesPathFlatten(
        new JmesPathProjection(
            new JmesPathField("groups"),
            new JmesPathField("members")
        )
    ),
    new JmesPathField("name")
);

Type Definitions

Expression Dependencies

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;

Advanced expressions work seamlessly with the Jackson JSON processing system and integrate with all other expression types for maximum flexibility in query construction.

Install with Tessl CLI

npx tessl i tessl/maven-com-amazonaws--jmespath-java

docs

advanced-expressions.md

built-in-functions.md

comparison-operations.md

core-expressions.md

expression-evaluation.md

index.md

tile.json