jsonGet function

JsonValue? jsonGet(
  1. JsonObject object,
  2. dynamic path
)

lodash.get-like accessor for deeply nested JsonObject values.

path may be a single String or int key, or an Iterable of keys for nested access.

Implementation

// ignore: no-object-declaration — path accepts String|int|Iterable intentionally
JsonValue jsonGet(JsonObject object, dynamic path) {
  Iterable<dynamic> list;
  if (path is String || path is int) {
    list = [path];
  } else {
    list = path as Iterable<dynamic>;
  }

  JsonValue result = object;
  for (final segment in list) {
    if (segment is int) {
      result = (result as JsonArray).elementAt(segment);
    } else {
      result = (result as JsonObject)[segment.toString()];
    }
  }

  return result;
}