I'm using the System.Text.Json package to work with/manipulate JSON and I've got a niche scenario where I can potentially reuse a method if I can provide it with a path to access the value of a node.
I noticed the JsonNode class has a GetPath() method which, as you'd expect, returns the path of the targeted node.
However, it looks like you can't use this output in reverse, meaning once you know the path you can't use it to access the value of a node.
For example, say my JSON looked like this:
{
"cakes": {
"cheese": "cake"
}
}
Calling node["cakes"]["cheese"] will result in "cake".
Whilst calling node["cakes"]["cheese"].GetPath() will return "$.cakes.cheese", the path to that "cake" value.
However, calling node["$.cakes.cheese"] just returns null where you'd expect it to return "cake".
Am I being a dumbass and using this incorrectly?
Or is the JsonNode class simply not intended to work this way, if so what's the point of the GetPath method?
Using the path is currently (.NET 7) not supported. It might be supported in a later version.
From documentation:
The JsonDocument DOM doesn't support querying by using JSON Path.
In a JsonNode DOM, each JsonNode instance has a GetPath method that returns a path to that node. But there is no built-in API to handle queries based on JSON Path query strings.
If you need very flexible Json support, use the Newtonsoft.Json Nuget package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With