Show / Hide Table of Contents

Class JsonSelector

Provides functionality for retrieving selected values from a root JsonElement.

Inheritance
System.Object
JsonSelector
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: JsonCons.JsonPath
Assembly: JsonCons.JsonPath.dll
Syntax
public sealed class JsonSelector
Examples

The following example shows how to select values, paths, and nodes from a JSON document

using System;
using System.Diagnostics;
using System.Text.Json;
using JsonCons.JsonPath;

public class Example
{
    public static void Main()
    {
        string jsonString = @"
{
    ""books"":
    [
        {
            ""category"": ""fiction"",
            ""title"" : ""A Wild Sheep Chase"",
            ""author"" : ""Haruki Murakami"",
            ""price"" : 22.72
        },
        {
            ""category"": ""fiction"",
            ""title"" : ""The Night Watch"",
            ""author"" : ""Sergei Lukyanenko"",
            ""price"" : 23.58
        },
        {
            ""category"": ""fiction"",
            ""title"" : ""The Comedians"",
            ""author"" : ""Graham Greene"",
            ""price"" : 21.99
        },
        {
            ""category"": ""memoir"",
            ""title"" : ""The Night Watch"",
            ""author"" : ""David Atlee Phillips"",
            ""price"" : 260.90
        }
    ]
}
        ");

        using JsonDocument doc = JsonDocument.Parse(jsonString);

        var options = new JsonSerializerOptions() {WriteIndented = true};

        // Selector of titles from union of all books with category 'memoir' 
        // and all books with price > 23
        var selector = JsonSelector.Parse("$.books[?@.category=='memoir',?@.price > 23].title");

        Console.WriteLine("Select values");
        IList<JsonElement> values = selector.Select(doc.RootElement);
        foreach (var value in values)
        {
            Console.WriteLine(JsonSerializer.Serialize(value, options));
        }
        Console.WriteLine();

        Console.WriteLine("Select paths");
        IList<JsonLocation> paths = selector.SelectPaths(doc.RootElement);
        foreach (var path in paths)
        {
            Console.WriteLine(path);
        }
        Console.WriteLine();

        Console.WriteLine("Select nodes");
        IList<PathValuePair> nodes = selector.SelectNodes(doc.RootElement);
        foreach (var node in nodes)
        {
            Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, options)}");
        }
        Console.WriteLine();

        Console.WriteLine("Remove duplicate nodes");
        IList<PathValuePair> uniqueNodes = selector.SelectNodes(doc.RootElement, 
                                                    new JsonSelectorOptions{NoDuplicates=true});
        foreach (var node in uniqueNodes)
        {
            Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, options)}");
        }
        Console.WriteLine();
    }
}

Output:

Select values
"The Night Watch"
"The Night Watch"
"The Night Watch"

Select paths
$['books'][3]['title']
$['books'][1]['title']
$['books'][3]['title']

Select nodes
$['books'][3]['title'] => "The Night Watch"
$['books'][1]['title'] => "The Night Watch"
$['books'][3]['title'] => "The Night Watch"

Remove duplicate nodes
$['books'][3]['title'] => "The Night Watch"
$['books'][1]['title'] => "The Night Watch"

Methods

| Improve this Doc View Source

Parse(String)

Parses a JSONPath string into a JsonSelector, for "parse once, use many times". A JsonSelector instance is thread safe and has no mutable state.

Declaration
public static JsonSelector Parse(string jsonPath)
Parameters
Type Name Description
System.String jsonPath

A JSONPath string.

Returns
Type Description
JsonSelector

A JsonSelector.

Exceptions
Type Condition
JsonPathParseException

The jsonPath parameter is not a valid JSONPath expression.

System.ArgumentNullException

The jsonPath is null.

| Improve this Doc View Source

Select(JsonElement, JsonSelectorOptions)

Selects values within the root value matched by this JSONPath expression.

Declaration
public IList<JsonElement> Select(JsonElement root, JsonSelectorOptions options = null)
Parameters
Type Name Description
System.Text.Json.JsonElement root

The root value.

JsonSelectorOptions options

Defines options for processing JSONPath queries.

Returns
Type Description
System.Collections.Generic.IList<System.Text.Json.JsonElement>

A list of values within the root value matched by this JSONPath expression, or an empty list if none were matched.

Exceptions
Type Condition
System.InvalidOperationException

Maximum depth level exceeded in recursive descent selector.

| Improve this Doc View Source

Select(JsonElement, String, JsonSelectorOptions)

Selects values within the root value matched by the provided JSONPath expression. This method parses and applies the expression in one operation.

Declaration
public static IList<JsonElement> Select(JsonElement root, string jsonPath, JsonSelectorOptions options = null)
Parameters
Type Name Description
System.Text.Json.JsonElement root

The root value.

System.String jsonPath

A JSONPath string.

JsonSelectorOptions options

Defines options for processing JSONPath queries.

Returns
Type Description
System.Collections.Generic.IList<System.Text.Json.JsonElement>

A list of values within the root value matched by the provided JSONPath expression, or an empty list if none were matched.

Exceptions
Type Condition
JsonPathParseException

The jsonPath parameter is not a valid JSONPath expression.

System.ArgumentNullException

jsonPath is null.

System.InvalidOperationException

Maximum depth level exceeded in recursive descent selector.

| Improve this Doc View Source

SelectNodes(JsonElement, JsonSelectorOptions)

Selects nodes that represent location-value pairs within the root value matched by this JSONPath expression.

Declaration
public IList<PathValuePair> SelectNodes(JsonElement root, JsonSelectorOptions options = null)
Parameters
Type Name Description
System.Text.Json.JsonElement root

The root value.

JsonSelectorOptions options

Defines options for processing JSONPath queries.

Returns
Type Description
System.Collections.Generic.IList<PathValuePair>

A list of PathValuePair representing location-value pairs within the root value matched by this JSONPath expression, or an empty list if none were matched.

| Improve this Doc View Source

SelectNodes(JsonElement, String, JsonSelectorOptions)

Selects nodes that represent location-value pairs within the root value matched by the JSONPath expression. This method parses and applies the expression in one operation.

Declaration
public static IList<PathValuePair> SelectNodes(JsonElement root, string jsonPath, JsonSelectorOptions options = null)
Parameters
Type Name Description
System.Text.Json.JsonElement root

The root value.

System.String jsonPath

A JSONPath string.

JsonSelectorOptions options

Defines options for processing JSONPath queries.

Returns
Type Description
System.Collections.Generic.IList<PathValuePair>

A list of PathValuePair representing location-value pairs within the root value matched by the provided JSONPath expression, or an empty list if none were matched.

Exceptions
Type Condition
JsonPathParseException

The jsonPath parameter is not a valid JSONPath expression.

System.ArgumentNullException

jsonPath is null.

System.InvalidOperationException

Maximum depth level exceeded in recursive descent selector.

| Improve this Doc View Source

SelectPaths(JsonElement, JsonSelectorOptions)

Selects paths identifying the values within the root value matched by this JSONPath expression.

Declaration
public IList<JsonLocation> SelectPaths(JsonElement root, JsonSelectorOptions options = null)
Parameters
Type Name Description
System.Text.Json.JsonElement root

The root value.

JsonSelectorOptions options

Defines options for processing JSONPath queries.

Returns
Type Description
System.Collections.Generic.IList<JsonLocation>

A list of JsonLocation identifying the values within the root value matched by this JSONPath expression, or an empty list if none were matched.

| Improve this Doc View Source

SelectPaths(JsonElement, String, JsonSelectorOptions)

Selects paths identifying the values within the root value matched by the JSONPath expression. This method parses and applies the expression in one operation.

Declaration
public static IList<JsonLocation> SelectPaths(JsonElement root, string jsonPath, JsonSelectorOptions options = null)
Parameters
Type Name Description
System.Text.Json.JsonElement root

The root value.

System.String jsonPath

A JSONPath string.

JsonSelectorOptions options

Defines options for processing JSONPath queries.

Returns
Type Description
System.Collections.Generic.IList<JsonLocation>

A list of JsonLocation identifying the values within the root value matched by the provided JSONPath expression, or an empty list if none were matched.

Exceptions
Type Condition
JsonPathParseException

The jsonPath parameter is not a valid JSONPath expression.

System.ArgumentNullException

jsonPath is null.

System.InvalidOperationException

Maximum depth level exceeded in recursive descent selector.

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX