Interface JsonExtract


public interface JsonExtract
A helper to extract values from a Map.

The path can be simple like "name" or a nested path using dot notation like "address.city".

For extracting numbers there are methods for int, long and double that will return the intValue(), longValue() and doubleValue() respectively.



   String json = "{\"name\":\"Rob\",\"score\":4.5,\"whenActive\":\"2025-10-20\",\"address\":{\"street\":\"Pall Mall\"}}";
   Map<String, Object> mapFromJson = jsonMapper.fromJsonObject(json);

   JsonExtract jsonExtract = jsonMapper.extract(mapFromJson);

   String name = jsonExtract.extract("name");
   double score = jsonExtract.extract("score", -1D);
   String street = jsonExtract.extract("address.street");

   LocalDate activeDate = jsonExtract.extractOrEmpty("whenActive")
     .map(LocalDate::parse)
     .orElseThrow();

 
  • Method Summary

    Modifier and Type
    Method
    Description
    Extract the text from the node at the given path.
    boolean
    extract(String path, boolean missingValue)
    Extract the boolean from the given path if present or the given default value.
    double
    extract(String path, double missingValue)
    Extract the double from the given path if present or the given default value.
    int
    extract(String path, int missingValue)
    Extract the int from the given path if present or the given default value.
    long
    extract(String path, long missingValue)
    Extract the long from the given path if present or the given default value.
    extract(String path, String missingValue)
    Extract the text value from the given path if present or the given default value.
    Extract the text value from the given path if present else empty.
    Return a JsonExtract for the given Map of values.
  • Method Details

    • of

      static JsonExtract of(Map<String,Object> map)
      Return a JsonExtract for the given Map of values.
    • extract

      Extract the text from the node at the given path.
      Throws:
      IllegalArgumentException - When the given path is missing.
    • extractOrEmpty

      Extract the text value from the given path if present else empty.
      
      
         LocalDate activeDate = jsonExtract.extractOrEmpty("whenActive")
           .map(LocalDate::parse)
           .orElseThrow();
      
       
    • extract

      String extract(String path, String missingValue)
      Extract the text value from the given path if present or the given default value.
      Parameters:
      missingValue - The value to use when the path is missing.
    • extract

      int extract(String path, int missingValue)
      Extract the int from the given path if present or the given default value.
      Parameters:
      missingValue - The value to use when the path is missing.
    • extract

      long extract(String path, long missingValue)
      Extract the long from the given path if present or the given default value.
      Parameters:
      missingValue - The value to use when the path is missing.
    • extract

      double extract(String path, double missingValue)
      Extract the double from the given path if present or the given default value.
      Parameters:
      missingValue - The value to use when the path is missing.
    • extract

      boolean extract(String path, boolean missingValue)
      Extract the boolean from the given path if present or the given default value.
      Parameters:
      missingValue - The value to use when the path is missing.