JsonCons.JsonPath
1.1.0
dotnet add package JsonCons.JsonPath --version 1.1.0
NuGet\Install-Package JsonCons.JsonPath -Version 1.1.0
<PackageReference Include="JsonCons.JsonPath" Version="1.1.0" />
paket add JsonCons.JsonPath --version 1.1.0
#r "nuget: JsonCons.JsonPath, 1.1.0"
// Install JsonCons.JsonPath as a Cake Addin #addin nuget:?package=JsonCons.JsonPath&version=1.1.0 // Install JsonCons.JsonPath as a Cake Tool #tool nuget:?package=JsonCons.JsonPath&version=1.1.0
JsonCons.JsonPath
The JsonCons.JsonPath library complements the functionality of the System.Text.Json namespace with an implementation of JSONPath. It targets .Net Standard 2.1.
The JsonCons.JsonPath library provides support for querying JsonDocument/JsonElement instances with code like this:
using System;
using System.Collections.Generic;
using System.Text.Json;
using JsonCons.JsonPath;
public static class Example
{
public static void Main(string[] args)
{
string jsonString = @"
{ ""store"": {
""book"": [
{ ""category"": ""reference"",
""author"": ""Nigel Rees"",
""title"": ""Sayings of the Century"",
""price"": 8.95
},
{ ""category"": ""fiction"",
""author"": ""Evelyn Waugh"",
""title"": ""Sword of Honour"",
""price"": 12.99
},
{ ""category"": ""fiction"",
""author"": ""Herman Melville"",
""title"": ""Moby Dick"",
""isbn"": ""0-553-21311-3"",
""price"": 8.99
},
{ ""category"": ""fiction"",
""author"": ""J. R. R. Tolkien"",
""title"": ""The Lord of the Rings"",
""isbn"": ""0-395-19395-8"",
""price"": 22.99
}
],
""bicycle"": {
""color"": ""red"",
""price"": 19.95
}
}
}
";
using JsonDocument doc = JsonDocument.Parse(jsonString);
IList<JsonElement> results = JsonSelector.Select(doc.RootElement, "$..book[?(@.price >= 5 && @.price < 10)]");
var serializerOptions = new JsonSerializerOptions() {WriteIndented = true};
Console.WriteLine(JsonSerializer.Serialize(results, serializerOptions));
}
}
Output:
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
]
JSONPath is a loosely standardized syntax for querying JSON. The original JavaScript JSONPath is a creation of Stefan Goessner and is described here. Since the original, there have been many implementations in multiple languages, implementations that differ in significant ways. For an exhaustive comparison of differences, see Christoph Burgmer's JSONPath comparison.
The JsonCons implementation attempts to preseve the essential flavor of JSONPath. Where implementations differ, it generally takes into account the consensus as established in the JSONPath comparison. It supports the familar queries against the store:
JSONPath | Result |
---|---|
$.store.book[*].author |
The authors of all books in the store |
$..author |
All authors |
$.store.* |
All things in store, which are some books and a red bicycle. |
$.store..price |
The price of everything in the store. |
$..book[2] |
The third book |
$..book[-1:] |
The last book in order. |
$..book[:2] |
The first two books |
$..book[0,1] |
|
$..book[?(@.isbn)] |
Filter all books with isbn number |
$..book[?(@.price<10)] |
Filter all books cheapier than 10 |
$..* |
All members of JSON structure. |
In addition, JsonCons.JsonPath incorporates some generalizations and tightening of syntax introduced in the more innovative and formally specified implementations.
Unquoted names follow the same rules for the selector and in filter expressions, and forbid characters such as hyphens that cause difficulties in expressions.
Names in the dot notation may be unquoted, single-quoted, or double-quoted.
Names in the square bracket notation may be single-quoted or double-quoted.
Like PaesslerAG/jsonpath/ajson, filter expressions may omit the parentheses around the expression, as in
$..book[?@.price<10]
.Unions may have separate JSONPath selectors, e.g.
$..[@.firstName,@.address.city]
A parent selector
^
, borrowed from JSONPath Plus, provides access to the parent node.Options are provided to exclude results corresponding to duplicate paths, and to sort results by paths.
The JsonCons implementation is described in an ABNF grammar with specification. It explicitly implements a state machine that corresponds to this grammar.
Documentation and examples may be found here:
Acknowledgements
Credit to Stefan Goessner's JsonPath, the original JSONPath. While not a formal specification, it was enormously influential.
The specification of JsonCons.JsonPath draws heavily on Christoph Burgmer's JSONPath Comparison. Many of the test cases and some of the examples are borrowed from this resource.
The specification of JsonCons.JsonPath filter expressions is greatly influenced by James Saryerwinnie's JMESPath, and largely follows JMSPath with regards to truthiness, comparator syntax and semantics, and function syntax and semantics. In Stefan Goessner's original article, filter expressions were left unspecified, and in their original implementations, they were simply Javascript or PHP.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- JsonCons.Utilities (>= 1.0.0)
- System.Text.Json (>= 5.0.2)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on JsonCons.JsonPath:
Package | Downloads |
---|---|
Microsoft.CST.ApplicationInspector.RulesEngine
Microsoft Application Inspector is a software source code analysis tool that helps identify and surface well-known features and other interesting characteristics of source code to aid in determining what the software is or what it does. |
|
Airbyte.Cdk
Package Description |
|
ConfirmSteps.Net
A simple testing suite to confirm the sequence of steps |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on JsonCons.JsonPath:
Repository | Stars |
---|---|
microsoft/ApplicationInspector
A source code analyzer built for surfacing features of interest and other characteristics to answer the question 'What's in the code?' quickly using static analysis with a json based rules engine. Ideal for scanning components before use or detecting feature level changes.
|
Version | Downloads | Last updated |
---|---|---|
1.1.0 | 130,557 | 8/25/2021 |
1.0.0 | 402 | 8/10/2021 |
1.0.0-preview.2 | 209 | 8/2/2021 |
1.0.0-preview.1 | 200 | 7/29/2021 |
Move from preview to release 1.0.0