exists method

bool exists(
  1. String path
)

Checks if the specified path exists within the JSON data.

This method traverses the JSON data along the specified path and returns true if all the keys in the path exist, and false otherwise.

If the JSON data at any point in the path is not a Map, this method will return false.

Implementation

bool exists(String path) {
  var current = json;
  final keys = path.split('.');

  for (var key in keys) {
    if (current == null) return false;

    // Handle array indices in path (e.g., "array.0.property")
    if (current is List && int.tryParse(key) != null) {
      final index = int.parse(key);
      if (index < 0 || index >= current.length) return false;
      current = current[index];
    } else if (current is Map) {
      if (!current.containsKey(key)) return false;
      current = current[key];
    } else {
      return false; // Can't navigate further
    }
  }
  return true;
}