Class JsonPathUtil

java.lang.Object
com.ultikits.ultitools.utils.JsonPathUtil

public final class JsonPathUtil extends Object
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 Details

    • JsonPathUtil

      private JsonPathUtil()
  • Method Details

    • getByPath

      public static Object getByPath(Map<String,Object> json, String path)
      Gets a value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path, e.g. "a.b.c"
      Returns:
      the value at the path, or null if the path does not exist or an intermediate node is not a Map
    • getStr

      public static String getStr(Map<String,Object> json, String path)
      Gets a String value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      the String value, or null if the value is absent
    • getInt

      public static Integer getInt(Map<String,Object> json, String path)
      Gets an Integer value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      the Integer value, or null if the value is absent or cannot be converted
    • getLong

      public static Long getLong(Map<String,Object> json, String path)
      Gets a Long value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      the Long value, or null if the value is absent or cannot be converted
    • getBool

      public static Boolean getBool(Map<String,Object> json, String path)
      Gets a Boolean value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      the Boolean value, or null if the value is absent
    • getDouble

      public static Double getDouble(Map<String,Object> json, String path)
      Gets a Double value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      the Double value, or null if the value is absent or cannot be converted
    • putByPath

      public static void putByPath(Map<String,Object> json, String path, Object value)
      Sets a value in a nested Map by path, automatically creating intermediate levels.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path, e.g. "a.b.c"
      value - the value to set
    • removeByPath

      public static Object removeByPath(Map<String,Object> json, String path)
      Removes a value in a nested Map by path.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      the removed value, or null if the path does not exist
    • containsPath

      public static boolean containsPath(Map<String,Object> json, String path)
      Checks whether a path exists.
      Parameters:
      json - the JSON Map object
      path - the dot-separated path
      Returns:
      true if the path exists and its value is not null