|
| 1 | +--- |
| 2 | +- id: 26b6ebca-58e6-4814-86ea-494ed844c9a8 |
| 3 | + background: | |
| 4 | + JSON Path is a syntax for querying JSON data. |
| 5 | +
|
| 6 | + _JsonPath.Net_ provides an implementation that conforms to the official IETF |
| 7 | + specification, [RFC 9535](https://www.rfc-editor.org/rfc/rfc9535.html). Like the other |
| 8 | + guides on this site, this guide will teach you how to use the library _JsonPath.Net_. |
| 9 | + |
| 10 | + However, because there are so few implementations of the RFC, and little to no |
| 11 | + documentation of it, this guide will also teach you the features of JSON Path itself, |
| 12 | + as described by the RFC. |
| 13 | +
|
| 14 | + We'll start with the library since there is less to cover, then we'll move on to |
| 15 | + what you can do with it. |
| 16 | +
|
| 17 | + Unlike JSON Schema, JSON Logic, or other technologies that are actually represented in |
| 18 | + JSON, JSON Path is its own syntax, so it must usually be parsed. The primary way to |
| 19 | + parse a path is using the static `JsonPath.Parse()` method. |
| 20 | + docs: 'path/basics' |
| 21 | + title: Parsing |
| 22 | + instructions: | |
| 23 | + Parse the given JSON Path text into a `path` variable. |
| 24 | + inputTemplate: '' |
| 25 | + contextCode: |- |
| 26 | + using System.Text.Json; |
| 27 | + using System.Text.Json.Nodes; |
| 28 | + using Json.Path; |
| 29 | + |
| 30 | + namespace LearnJsonEverything; |
| 31 | + |
| 32 | + public class Lesson : ILessonRunner<PathResult> |
| 33 | + { |
| 34 | + public PathResult Run(JsonObject test) |
| 35 | + { |
| 36 | + var data = test["data"]; |
| 37 | + var pathText = "$.foo.bar"; |
| 38 | +
|
| 39 | + /* USER CODE */ |
| 40 | +
|
| 41 | + return path.Evaluate(data); |
| 42 | + } |
| 43 | + } |
| 44 | + solution: |- |
| 45 | + var path = JsonPath.Parse(pathText); |
| 46 | + tests: |
| 47 | + - data: { "foo": { "bar": "a string" } } |
| 48 | + result: ['a string'] |
0 commit comments