JsonHelper.Net
1.1.5
dotnet add package JsonHelper.Net --version 1.1.5
NuGet\Install-Package JsonHelper.Net -Version 1.1.5
<PackageReference Include="JsonHelper.Net" Version="1.1.5" />
<PackageVersion Include="JsonHelper.Net" Version="1.1.5" />
<PackageReference Include="JsonHelper.Net" />
paket add JsonHelper.Net --version 1.1.5
#r "nuget: JsonHelper.Net, 1.1.5"
#:package JsonHelper.Net@1.1.5
#addin nuget:?package=JsonHelper.Net&version=1.1.5
#tool nuget:?package=JsonHelper.Net&version=1.1.5
JsonHelper.Net
JsonHelper.Net provides typed, null-safe JSONPath querying for Newtonsoft.Json. Eliminate repetitive null checks and type validation when extracting values from JToken objects.
Features
- Typed selectors — Extract
string,bool,int,float,DateTime,Guid,List<T>, or rawJTokendirectly. - Two usage styles — Use
JsonHelper.SelectString(json, path)as a static helper orjtoken.SelectString(path)as a fluent extension method. - Nullable vs. throwing — Every type provides a nullable method (returns
nullon missing/wrong type) and anOrThrowvariant (throwsJsonHelperExceptionfor strict validation). - Custom date parsing —
SelectDate(path, format)parses date strings from arbitrary formats viaDateTime.ParseExact. - Type-safe JToken access —
SelectOrThrow(path, JTokenType.Array)validates the JSON token type at runtime. - Built on Newtonsoft.Json 13.0.4 — Targets .NET 7 with nullable reference type annotations.
Quick start
using Newtonsoft.Json.Linq;
using NewtonsoftJsonHelper;
var json = JToken.Parse("""
{
"name": "Alice",
"age": 30,
"active": true,
"score": 98.5,
"joined": "2024-01-15T00:00:00",
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
""");
// --- Nullable style (returns null on missing/null/wrong type) ---
string? maybe = json.SelectString("$.name"); // "Alice"
int? age = JsonHelper.SelectInt(json, "$.age"); // 30
bool? admin = json.SelectBoolean("$.admin"); // null (missing path)
// --- Throwing style (throws JsonHelperException on failure) ---
string name = json.SelectStringOrThrow("$.name"); // "Alice"
float score = json.SelectFloatOrThrow("$.score"); // 98.5
Guid id = json.SelectGuidOrThrow("$.id"); // Guid
DateTime dt = JsonHelper.SelectDateOrThrow(json, "$.joined"); // DateTime
// --- Without JsonHelper.Net (verbose boilerplate) ---
var nameToken = json.SelectToken("$.name");
var nameValue = nameToken?.ToObject<string>();
if (nameToken == null || nameValue == null)
throw new Exception("Expected 'name' token");
API Reference
All methods exist in two forms — static JsonHelper and fluent JToken extension methods:
| Category | Static (JsonHelper) |
Extension (JTokenExtensions) |
|---|---|---|
| String | SelectString(json, path) / SelectStringOrThrow(json, path) |
SelectString(path) / SelectStringOrThrow(path) |
| Boolean | SelectBoolean(json, path) / SelectBooleanOrThrow(json, path) |
SelectBoolean(path) / SelectBooleanOrThrow(path) |
| Integer | SelectInt(json, path) / SelectIntOrThrow(json, path) |
SelectInt(path) / SelectIntOrThrow(path) |
| Float | SelectFloat(json, path) / SelectFloatOrThrow(json, path) |
SelectFloat(path) / SelectFloatOrThrow(path) |
| DateTime | SelectDate(json, path) / SelectDateOrThrow(json, path) |
SelectDate(path) / SelectDateOrThrow(path) |
| DateTime (format) | SelectDate(json, path, format) / SelectDateOrThrow(json, path, format) |
SelectDate(path, format) / SelectDateOrThrow(path, format) |
| Guid | SelectGuid(json, path) / SelectGuidOrThrow(json, path) |
SelectGuid(path) / SelectGuidOrThrow(path) |
| List<T> | SelectList\<T\>(json, path) / SelectListOrThrow\<T\>(json, path) |
SelectList\<T\>(path) / SelectListOrThrow\<T\>(path) |
| JToken | Select(json, path, params JTokenType[]) / SelectOrThrow(json, path, params JTokenType[]) |
Select(path, params JTokenType[]) / SelectOrThrow(path, params JTokenType[]) |
- Nullable methods return
T?and returnnullwhen the token is missing, null, or of the wrong type. - OrThrow methods return
T(non-nullable) and throwJsonHelperExceptionwhen the token cannot be resolved.
Advanced usage
Date with custom format
var json = JToken.Parse("""{"published": "12/25/2023"}""");
// Parse a string using DateTime.ParseExact
DateTime christmas = json.SelectDateOrThrow("$.published", "MM/dd/yyyy");
List of values
var json = JToken.Parse("""{"ratings": [4.5, 3.8, 5.0, 2.1]}""");
List<float> ratings = json.SelectListOrThrow<float>("$.ratings");
Raw JToken with type validation
var json = JToken.Parse("""{"metadata": {"key": "value"}}""");
// Validates the token is an object, returns the JToken
JToken meta = json.SelectOrThrow("$.metadata", JTokenType.Object);
// Works with multiple acceptable types
JToken field = json.Select("$.metadata", JTokenType.Object, JTokenType.String);
Why JsonHelper.Net?
Without this library, extracting typed values from JToken requires manual null checks and type validation every time:
// Manual checks every time
var token = json.SelectToken("$.some.path");
string? value = null;
if (token != null && token.Type == JTokenType.String)
value = token.ToObject<string>();
if (value == null)
throw new Exception("Expected string at $.some.path");
With JsonHelper.Net this becomes a single call:
string value = json.SelectStringOrThrow("$.some.path");
The library handles:
- JSONPath resolution
- Null checks (
SelectTokencan returnnull) - Type validation (checks the
JTokenTypebefore converting) - Parsing conversions (Guid from string, DateTime from string with format)
- Clear exception messages on failure
Installation
Nuget page is here
Contribution
Pull requests are welcome! I will gladly review any feature suggestions and consider them for inclusion in the library.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Newtonsoft.Json (>= 13.0.4)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on JsonHelper.Net:
| Package | Downloads |
|---|---|
|
Hometrica.Sdk
.NET Sdk for Hometrica.Server |
|
|
Viklover.Radsel
Radsel CCU API .NET Client |
|
|
Viklover.Seaweed
SeaweedFS .NET driver |
GitHub repositories
This package is not used by any popular GitHub repositories.