Get Started
| Product | Versions |
|---|---|
| .NET Standard | |
| Other |
README
JsonPtr
This project provides extension methods for the System.Text.Json.JsonElement type to support JSON Pointer (RFC 6901) resolution.
Features
- JSON Pointer Support: Resolve JSON elements using RFC 6901-compliant JSON Pointer strings.
- Flexible APIs:
GetElement: Retrieves a JSON element or throws aJsonPointerExceptionon failure.GetElementOrNull: Retrieves a JSON element or returns null on failure.TryGetElement: Attempts to retrieve a JSON element, returning a success flag and error details if applicable.
Usage
GetElement
Retrieves a JSON element at the specified pointer, throwing an exception if resolution fails.
var json = @"{""person"":{""name"":""John""}}";
var element = JsonDocument.Parse(json).RootElement;
try
{
var nameElement = element.GetElement("/person/name");
Console.WriteLine(nameElement.GetString()); // Output: John
var invalidElement = element.GetElement("/person/age");
}
catch (JsonPointerException ex)
{
Console.WriteLine(ex.Message); // Handles invalid pointers
}
GetElementOrNull
Retrieves a JSON element or returns null if the pointer cannot be resolved.
using System.Text.Json;
var json = @"{""person"":{""name"":""John""}}";
var element = JsonDocument.Parse(json).RootElement;
var name = element.GetElementOrNull("/person/name");
Console.WriteLine(name.Value.GetString()); // Output: John
var age = element.GetElementOrNull("/person/age");
Console.WriteLine(age.HasValue ? age.Value.GetString() : "Not found"); // Output: Not found
TryGetElement
Attempts to retrieve a JSON element, returning success status and error details.
using System.Text.Json;
var json = @"{""person"":{""name"":""John""}}";
var element = JsonDocument.Parse(json).RootElement;
if (element.TryGetElement("/person/name", out JsonElement? result, out JsonPointerError? error))
{
Console.WriteLine(result.Value.GetString()); // Output: John
}
else
{
Console.WriteLine(error.Message); // Handles failure
}
JSON Pointer Syntax
/propertyaccesses an object property./0accesses an array element by index.- Use
~0for~and~1for/in escaped keys (e.g.,/foo~1barfor keyfoo/bar). - Empty pointer (
"") refers to the root element.
Downloads by Version
Similar & Alternative Packages
Hand-picked matching NuGet libraries based on tags & ecosystem
Depended On By
Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.