C# and .NET examples · JSON and data
JSONPath Queries in C# with Newtonsoft.Json
A .NET 6 console application demonstrating 14 JSONPath query patterns in C# using Newtonsoft.Json’s JObject.SelectTokens method — wildcards, array indexes, recursive descent, filter expressions with && and ||, and regular-expression matches. The project serves as a practical reference for navigating complex, nested JSON structures.
In this guide: Get started · Walk through the example · Understand the details · Check the limits
🔎 Overview
JSONPath (via JObject.SelectTokens) selects values directly from JSON objects and arrays, including nested properties and filter expressions. This project runs fourteen queries against a sample person record (Data.json) and prints the results to the console.
🚀 Quick start
Build and Run
cd JsonSearchPatterns
dotnet run
The application prints the results of each query to the console, separated by blank lines.
📦 Prerequisites and installation
Requirements
- .NET 6 SDK plus a compatible .NET 6 runtime
NuGet Packages
| Package | Version |
|---|---|
Newtonsoft.Json |
13.0.3 |
💡 Usage
Query Patterns Demonstrated
| Pattern | Description |
|---|---|
$.* |
All root-level elements |
$.education |
Specific property at the root level |
$.parents[*] |
All elements of the parents array |
$.parents[?(@.relationship == 'father')] |
Array filter by property equality |
$.parents[?(@.relationship == 'father' && @.age == 40)] |
Array filter with AND condition |
$..* |
All elements at any depth (recursive descent) |
$..location |
All location properties at any depth |
$.communication.addresses.location[*] |
All elements of a nested array, reached by an explicit path |
$.parents..relationship |
Recursive descent within the parents subtree |
$..personal..number |
Nested recursive search |
$.parents[0] |
First element of an array by index |
$..location[0] |
First location at any depth |
$.parents[?(@.name =~ /^J/)] |
Array filter using a regular expression |
$.parents[?(@.occupation == 'Teacher' \|\| @.age >= 40)] |
Array filter with OR condition |
Sample Data (Data.json)
The JSON file describes a person with:
name,agecommunication— email addresses, location (city, coordinates, postal code), phone numberseducation— institution, degrees, majorparents— array of two objects (father and mother) with name, age, occupation, email, and relationshipmiscellaneous— array of hobby entriesmessageHeader— id and created date
🏗️ Architecture
Project Structure
JsonSearchPatterns/
├── Data.json # Sample person JSON document
├── FileHandler.cs # Reads Data.json and runs SelectTokens queries
├── Program.cs # Executes all query patterns and prints results
├── JsonSearchPatterns.csproj
└── JsonSearchPatterns.sln
🧪 Development and testing
Run the sample from its folder and compare all fourteen queries with Data.json, including the AND/OR and regex filters. Confirm that changing the working directory without the data file produces the documented limitation. The review checked the query strings and file-copy setting; it did not execute the console sample.
🛠️ Troubleshooting and limitations
FAQ
What is the difference between SelectToken and SelectTokens?
SelectToken returns a single matching token (or null), while SelectTokens returns every match as an IEnumerable<JToken>. This project uses SelectTokens so that wildcard, recursive, and filter queries can return several results.
Can I run the same queries with System.Text.Json?
These examples rely on Newtonsoft.Json’s built-in JSONPath support through SelectTokens; System.Text.Json does not offer this method.
FileHandler reads Data.json relative to the current working directory on every query. Run from this sample folder as shown, or from an output folder containing the copied file; running with –project from an unrelated folder can fail to find it. The application waits for console input after printing results.
🔗 Related documentation
Related Examples
Documentation links
Read all C# and .NET Code Examples guides online · Project overview · All developer projects