Class JsonSelector
Provides functionality for retrieving selected values from a root JsonElement.
Inheritance
Inherited Members
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 SourceParse(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 |
System.ArgumentNullException | The |
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. |
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 |
System.ArgumentNullException |
|
System.InvalidOperationException | Maximum depth level exceeded in recursive descent selector. |
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. |
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 |
System.ArgumentNullException |
|
System.InvalidOperationException | Maximum depth level exceeded in recursive descent selector. |
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. |
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 |
System.ArgumentNullException |
|
System.InvalidOperationException | Maximum depth level exceeded in recursive descent selector. |