Package io.avaje.json.mapper
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 TypeMethodDescriptionExtract the text from the node at the given path.boolean
Extract the boolean from the given path if present or the given default value.double
Extract the double from the given path if present or the given default value.int
Extract the int from the given path if present or the given default value.long
Extract the long from the given path if present or the given default value.Extract the text value from the given path if present or the given default value.extractOrEmpty
(String path) Extract the text value from the given path if present else empty.static JsonExtract
Return a JsonExtract for the given Map of values.
-
Method Details
-
of
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
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
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
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
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
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.
-