JSONPath Functions
JsonPath expressions support basic method calls.
JSONPath Extensions Functions
You can extend the supported function set by registering your own functions.
Example: Implement a JsonNode Path Function:
Step 1: Create a custom function that returns the path of a JsonNode.
public class PathNodeFunction() : ExtensionFunction( PathMethod, CompareConstraint.MustCompare )
{
public const string Name = "path";
private static readonly MethodInfo PathMethod = GetMethod<PathNodeFunction>( nameof( Path ) );
private static ScalarValue<string> Path( IValueType argument )
{
return argument.TryGetNode<JsonNode>( out var node ) ? node?.GetPath() : null;
}
}
Step 2: Register your custom function.
JsonTypeDescriptorRegistry.GetDescriptor<JsonNode>().Functions
.Register( PathNodeFunction.Name, () => new PathNodeFunction() );
Step 3: Use your custom function in a JSONPath query.
var results = source.Select( "$..[?path(@) == '$.store.book[2].title']" );
