Class JsonPathUtils
This class simplifies working with complex JSON structures by providing type-safe accessors and path-based navigation. It supports dot notation for accessing nested properties and numeric indices for array elements.
Path notation examples:
- "field" - Access a top-level field
- "parent.child" - Access a nested field
- "array.0" - Access the first element of an array
- "parent.array.2.field" - Complex nested path with array access
The class provides:
- Type-safe getters for primitives (string, int, boolean)
- Optional variants for graceful handling of missing values
- Collection utilities for working with arrays
- Path existence checking
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionOptional
<com.fasterxml.jackson.databind.JsonNode> findInArray
(com.fasterxml.jackson.databind.JsonNode root, String arrayPath, Function<com.fasterxml.jackson.databind.JsonNode, Boolean> predicate) Finds the first element in a JSON array that matches the given predicate.void
forEachInArray
(com.fasterxml.jackson.databind.JsonNode root, String arrayPath, Consumer<com.fasterxml.jackson.databind.JsonNode> consumer) Iterates over each element in a JSON array, applying the given consumer.com.fasterxml.jackson.databind.node.ArrayNode
Get an array node from a pathboolean
getBoolean
(com.fasterxml.jackson.databind.JsonNode root, String path) Get a boolean value from a JSON pathgetFieldNames
(com.fasterxml.jackson.databind.JsonNode root, String objectPath) Retrieves all field names from a JSON object at the specified path.int
Get an integer value from a JSON pathcom.fasterxml.jackson.databind.JsonNode
Navigates to and retrieves a JSON node at the specified path.com.fasterxml.jackson.databind.node.ObjectNode
Get an object node from a pathOptional
<com.fasterxml.jackson.databind.node.ArrayNode> getOptionalArray
(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional array node from a pathgetOptionalBoolean
(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional boolean value from a JSON pathgetOptionalInt
(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional integer value from a JSON pathOptional
<com.fasterxml.jackson.databind.node.ObjectNode> getOptionalObject
(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional object node from a pathgetOptionalString
(com.fasterxml.jackson.databind.JsonNode root, String path) Retrieves an optional string value from a JSON path.Retrieves a string value from a JSON path.boolean
Checks if a path exists in the JSON structure.<T> List
<T> mapArray
(com.fasterxml.jackson.databind.JsonNode root, String arrayPath, Function<com.fasterxml.jackson.databind.JsonNode, T> mapper) Transforms each element in a JSON array into a list of typed objects.
-
Constructor Details
-
JsonPathUtils
public JsonPathUtils()
-
-
Method Details
-
getString
public String getString(com.fasterxml.jackson.databind.JsonNode root, String path) throws ConfigurationException Retrieves a string value from a JSON path.The path must point to a text node. If the node exists but is not textual (e.g., number, boolean, object, array), a ConfigurationException is thrown.
- Parameters:
root
- The root JSON node to start navigation frompath
- The dot-notation path to the value (e.g., "config.name")- Returns:
- The string value at the specified path
- Throws:
ConfigurationException
- if the path doesn't exist or the value is not a string
-
getOptionalString
public Optional<String> getOptionalString(com.fasterxml.jackson.databind.JsonNode root, String path) Retrieves an optional string value from a JSON path.This method provides a null-safe way to access string values. If the path doesn't exist or the value is not a string, an empty Optional is returned instead of throwing an exception.
- Parameters:
root
- The root JSON node to start navigation frompath
- The dot-notation path to the value- Returns:
- An Optional containing the string value, or empty if not found/not a string
-
getInt
public int getInt(com.fasterxml.jackson.databind.JsonNode root, String path) throws ConfigurationException Get an integer value from a JSON path- Parameters:
root
- The root JSON nodepath
- The path to the value (dot notation)- Returns:
- The integer value
- Throws:
ConfigurationException
- if the path doesn't exist or the value is not an integer
-
getOptionalInt
Get an optional integer value from a JSON path- Parameters:
root
- The root JSON nodepath
- The path to the value (dot notation)- Returns:
- An optional integer value
-
getBoolean
public boolean getBoolean(com.fasterxml.jackson.databind.JsonNode root, String path) throws ConfigurationException Get a boolean value from a JSON path- Parameters:
root
- The root JSON nodepath
- The path to the value (dot notation)- Returns:
- The boolean value
- Throws:
ConfigurationException
- if the path doesn't exist or the value is not a boolean
-
getOptionalBoolean
public Optional<Boolean> getOptionalBoolean(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional boolean value from a JSON path- Parameters:
root
- The root JSON nodepath
- The path to the value (dot notation)- Returns:
- An optional boolean value
-
getNode
public com.fasterxml.jackson.databind.JsonNode getNode(com.fasterxml.jackson.databind.JsonNode root, String path) throws ConfigurationException Navigates to and retrieves a JSON node at the specified path.This is the core navigation method that supports:
- Dot notation for object field access
- Numeric indices for array element access
- Mixed paths combining objects and arrays
Examples:
- "field" - Returns root.field
- "parent.child" - Returns root.parent.child
- "items.0.name" - Returns root.items[0].name
- Parameters:
root
- The root JSON node to start navigation frompath
- The dot-notation path to navigate (empty/null returns root)- Returns:
- The JsonNode at the specified path
- Throws:
ConfigurationException
- if any part of the path doesn't exist or is invalid
-
getArray
public com.fasterxml.jackson.databind.node.ArrayNode getArray(com.fasterxml.jackson.databind.JsonNode root, String path) throws ConfigurationException Get an array node from a path- Parameters:
root
- The root JSON nodepath
- The path to the array (dot notation)- Returns:
- The array node
- Throws:
ConfigurationException
- if the path doesn't exist or the node is not an array
-
getOptionalArray
public Optional<com.fasterxml.jackson.databind.node.ArrayNode> getOptionalArray(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional array node from a path- Parameters:
root
- The root JSON nodepath
- The path to the array (dot notation)- Returns:
- An optional array node
-
getObject
public com.fasterxml.jackson.databind.node.ObjectNode getObject(com.fasterxml.jackson.databind.JsonNode root, String path) throws ConfigurationException Get an object node from a path- Parameters:
root
- The root JSON nodepath
- The path to the object (dot notation)- Returns:
- The object node
- Throws:
ConfigurationException
- if the path doesn't exist or the node is not an object
-
getOptionalObject
public Optional<com.fasterxml.jackson.databind.node.ObjectNode> getOptionalObject(com.fasterxml.jackson.databind.JsonNode root, String path) Get an optional object node from a path- Parameters:
root
- The root JSON nodepath
- The path to the object (dot notation)- Returns:
- An optional object node
-
hasPath
Checks if a path exists in the JSON structure.This method provides a safe way to check path existence without throwing exceptions. It's useful for conditional logic based on JSON structure.
- Parameters:
root
- The root JSON node to checkpath
- The dot-notation path to verify- Returns:
- true if the complete path exists and is navigable, false otherwise
-
forEachInArray
public void forEachInArray(com.fasterxml.jackson.databind.JsonNode root, String arrayPath, Consumer<com.fasterxml.jackson.databind.JsonNode> consumer) throws ConfigurationException Iterates over each element in a JSON array, applying the given consumer.This method provides a functional approach to array processing, allowing side effects for each element without collecting results.
- Parameters:
root
- The root JSON node containing the arrayarrayPath
- The dot-notation path to the arrayconsumer
- The operation to perform on each array element- Throws:
ConfigurationException
- if the path doesn't exist or doesn't point to an array
-
mapArray
public <T> List<T> mapArray(com.fasterxml.jackson.databind.JsonNode root, String arrayPath, Function<com.fasterxml.jackson.databind.JsonNode, T> mapper) throws ConfigurationExceptionTransforms each element in a JSON array into a list of typed objects.This method provides a functional transformation from JSON array elements to a strongly-typed Java list. The mapper function defines how each JsonNode element is converted to the target type.
- Type Parameters:
T
- The target type for list elements- Parameters:
root
- The root JSON node containing the arrayarrayPath
- The dot-notation path to the arraymapper
- The transformation function for each element- Returns:
- A list containing the transformed elements
- Throws:
ConfigurationException
- if the path doesn't exist or doesn't point to an array
-
findInArray
public Optional<com.fasterxml.jackson.databind.JsonNode> findInArray(com.fasterxml.jackson.databind.JsonNode root, String arrayPath, Function<com.fasterxml.jackson.databind.JsonNode, Boolean> predicate) throws ConfigurationExceptionFinds the first element in a JSON array that matches the given predicate.This method provides a functional search mechanism for JSON arrays, returning the first element that satisfies the predicate condition. The search stops as soon as a match is found (short-circuit evaluation).
- Parameters:
root
- The root JSON node containing the arrayarrayPath
- The dot-notation path to the arraypredicate
- The condition to test each element against- Returns:
- An Optional containing the first matching element, or empty if no matches
- Throws:
ConfigurationException
- if the path doesn't exist or doesn't point to an array
-
getFieldNames
public List<String> getFieldNames(com.fasterxml.jackson.databind.JsonNode root, String objectPath) throws ConfigurationException Retrieves all field names from a JSON object at the specified path.This method is useful for dynamic JSON processing where field names are not known at compile time. It returns the names in iteration order, which may not be the same as the order in the original JSON text.
- Parameters:
root
- The root JSON node to navigate fromobjectPath
- The dot-notation path to the object (empty for root)- Returns:
- A list of field names present in the object
- Throws:
ConfigurationException
- if the path doesn't exist or doesn't point to an object
-