JsonCons.Net
The JsonCons.Net libraries complement the System.Text.Json namespace with support for JSON Pointer, JSON Patch, JSON Merge Patch, JSONPath, and JMESPath.
Install / Use
/learn @danielaparker/JsonCons.NetREADME
JsonCons.Net
|JsonCons.Utilities|JsonCons.JsonPath|JsonCons.JmesPath| |:-:|:-:|:-:| |<a href="https://www.nuget.org/packages/JsonCons.Utilities/"><img alt="NuGet version" src="https://img.shields.io/nuget/v/JsonCons.Utilities.svg?svg=true"></img><br><img alt="NuGet version" src="https://img.shields.io/nuget/dt/JsonCons.Utilities.svg?svg=true"></img></a>|<a href="https://www.nuget.org/packages/JsonCons.JsonPath/"><img alt="NuGet version" src="https://img.shields.io/nuget/v/JsonCons.JsonPath.svg?svg=true"></img><br><img alt="NuGet version" src="https://img.shields.io/nuget/dt/JsonCons.JsonPath.svg?svg=true"></img></a>|<a href="https://www.nuget.org/packages/JsonCons.JmesPath/"><img alt="NuGet version" src="https://img.shields.io/nuget/v/JsonCons.JmesPath.svg?svg=true"></img><br><img alt="NuGet version" src="https://img.shields.io/nuget/dt/JsonCons.JmesPath.svg?svg=true"></img></a>|
The JsonCons.Net libraries include classes that complement the functionality of the System.Text.Json namespace. The libraries target .Net Standard 2.1.
The JsonCons.Net libraries offer support for:
- JSON Pointer as defined in RFC 6901
- JSON Patch as defined in RFC 6902
- JSON Merge Patch as defined in RFC 7396
- JSONPath as defined in JsonCons JsonPath
- JMESPath as defined in JMESPath Specification
JSONPath and JMESPath
JSONPath allows you to select from a JsonDocument a list of JsonElement instances that belong to it. JMESPath allows you to transform a JsonDocument into another JsonDocument.
For example, consider the JSON data
string jsonString = @"
{
""Data"":[
{
""KeyOfInterest"":true,
""AnotherKey"":true
},
{
""KeyOfInterest"":false,
""AnotherKey"":true
},
{
""KeyOfInterest"":true,
""AnotherKey"":true
}
]
}
";
using JsonDocument doc = JsonDocument.Parse(jsonString);
JSONPath allows you to select the KeyOfInterest values like this:
string path = "$.Data[*].KeyOfInterest";
IList<JsonElement> results = JsonSelector.Select(doc.RootElement, path);
and the union of KeyOfInterest and AnotherKey values like this:
string path = "$.Data[*]['KeyOfInterest', 'AnotherKey']";
IList<JsonElement> results = JsonSelector.Select(doc.RootElement, path);
The first query produces
[true,false,true]
and the second
[true,true,false,true,true,true]
Note that each element in the result - true, false, true - corresponds to an element
at a specific location in the original JSON document. This is a feature of JSONPath.
JMESPath allows you to select the KeyOfInterest values like this:
string expr = Data[*].KeyOfInterest;
JsonDocument result = JsonTransformer.Transform(doc.RootElement, expr);
and a multiselect hash of KeyOfInterest and AnotherKey values like this:
string expr = "Data[*].{\"Key of Interest\" : KeyOfInterest, \"Another Key\": AnotherKey}";
JsonDocument result = JsonTransformer.Transform(doc.RootElement, expr);
The first query produces
[true,false,true]
and the second
[
{
"Key of Interest": true,
"Another Key": true
},
{
"Key of Interest": false,
"Another Key": true
},
{
"Key of Interest": true,
"Another Key": true
}
]
JMESPath, unlike JSONPath, can create new elements that are not in the original document. JMESPath can transform, while JsonPath can only select.
Documentation and Examples
Reference documentation is available here
Code examples may be found at:
Related Skills
openhue
338.7kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
338.7kElevenLabs text-to-speech with mac-style say UX.
weather
338.7kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.5kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
