readPath function

Object? readPath(
  1. JsonMap json,
  2. String path
)

Reads the value at path from a JSON document using dot notation.

Returns null if the path does not exist. Paths like 'user.name' will navigate through nested objects.

Implementation

Object? readPath(JsonMap json, String path) {
  if (path.isEmpty) {
    return json;
  }

  Object? current = json;
  for (final segment in path.split('.')) {
    if (current is Map<dynamic, dynamic>) {
      current = current[segment];
    } else {
      return null;
    }
  }

  return current;
}