Package com.ultikits.ultitools.utils
Class JsonPathUtil
java.lang.Object
com.ultikits.ultitools.utils.JsonPathUtil
JSON path utility class - replaces hutool-json's putByPath/getByPath functionality
Provides dot-separated path access and mutation of values inside a nested JSON Map.
Usage example: Map<String, Object> json = new LinkedHashMap<>(); JsonPathUtil.putByPath(json, "a.b.c", "value"); Object value = JsonPathUtil.getByPath(json, "a.b.c"); // "value"
- Since:
- 6.2.0
- Author:
- UltiKits Team
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsPath(Map<String, Object> json, String path) Checks whether a path exists.static BooleanGets a Boolean value in a nested Map by path.static ObjectGets a value in a nested Map by path.static DoubleGets a Double value in a nested Map by path.static IntegerGets an Integer value in a nested Map by path.static LongGets a Long value in a nested Map by path.static StringGets a String value in a nested Map by path.static voidSets a value in a nested Map by path, automatically creating intermediate levels.static ObjectremoveByPath(Map<String, Object> json, String path) Removes a value in a nested Map by path.
-
Constructor Details
-
JsonPathUtil
private JsonPathUtil()
-
-
Method Details
-
getByPath
Gets a value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path, e.g. "a.b.c"- Returns:
- the value at the path, or
nullif the path does not exist or an intermediate node is not a Map
-
getStr
Gets a String value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
- the String value, or
nullif the value is absent
-
getInt
Gets an Integer value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
- the Integer value, or
nullif the value is absent or cannot be converted
-
getLong
Gets a Long value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
- the Long value, or
nullif the value is absent or cannot be converted
-
getBool
Gets a Boolean value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
- the Boolean value, or
nullif the value is absent
-
getDouble
Gets a Double value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
- the Double value, or
nullif the value is absent or cannot be converted
-
putByPath
Sets a value in a nested Map by path, automatically creating intermediate levels.- Parameters:
json- the JSON Map objectpath- the dot-separated path, e.g. "a.b.c"value- the value to set
-
removeByPath
Removes a value in a nested Map by path.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
- the removed value, or
nullif the path does not exist
-
containsPath
Checks whether a path exists.- Parameters:
json- the JSON Map objectpath- the dot-separated path- Returns:
trueif the path exists and its value is notnull
-