<PackageReference Include="NJsonSchema" Version="8.23.6298.10815" />

JsonPathUtilities

public static class JsonPathUtilities
Utilities to work with JSON paths.
using NJsonSchema.Infrastructure; using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace NJsonSchema { public static class JsonPathUtilities { public static string GetJsonPath(object rootObject, object searchedObject) { if (rootObject == null) throw new ArgumentNullException("rootObject"); string jsonPath = GetJsonPath(rootObject, searchedObject, "#", new HashSet<object>()); if (jsonPath == null) throw new InvalidOperationException("Could not find the JSON path of a referenced schema: Manually referenced schemas must be added to the 'Definitions' of a parent schema."); return jsonPath; } private static string GetJsonPath(object obj, object searchedObject, string basePath, HashSet<object> checkedObjects) { if (obj == null || obj is string || checkedObjects.Contains(obj)) return null; if (obj == searchedObject) return basePath; checkedObjects.Add(obj); if (obj is IDictionary) { foreach (object key in ((IDictionary)obj).Keys) { string jsonPath = GetJsonPath(((IDictionary)obj)[key], searchedObject, basePath + "/" + key, checkedObjects); if (jsonPath != null) return jsonPath; } } else if (obj is IEnumerable) { int num = 0; foreach (object item in (IEnumerable)obj) { string jsonPath2 = GetJsonPath(item, searchedObject, basePath + "/" + num, checkedObjects); if (jsonPath2 != null) return jsonPath2; num++; } } else { foreach (ReflectionCache.PropertyOrField item2 in from p in ReflectionCache.GetPropertiesAndFields(obj.GetType()) where p.CustomAttributes.JsonIgnoreAttribute == null select p) { object value = item2.GetValue(obj); if (value != null) { string name = item2.GetName(); string jsonPath3 = GetJsonPath(value, searchedObject, basePath + "/" + name, checkedObjects); if (jsonPath3 != null) return jsonPath3; } } } return null; } } }