Given the following JSON:
{
"pos":123,
"name":"Bla"
}
I'm trying to validate that the JSON provided has a name property with the value Bla
Following the documentation on json.net I established that this should work:
$.[?(@.name == 'Bla')]
I validated this idea on https://jsonpath.herokuapp.com/ and it seems to return the object if it is true, and nothing if it isn't
However, when trying the same in json.net it never seems to return a valid JToken.
Example code here: https://dotnetfiddle.net/0agXyZ (passing true into SelectToken says it should throw an error if the result is not found, I'm not seeing this either).
Is this something I'm doing wrong here?
What I'm trying to do is write a test That will allow developers to specify expected JSON to be returned from their tests, so it made sense to me that if there was a way to provide them a way to input a validation string in JSON path format I can hide the details of this validation in my framework.
Thanks.
SelectToken()is only designed to work for objects inside arrays, not for root objects or objects inside other objects. See: JSONPath scripts not executing correctly for objects #1256: I'm not sure about this. Nothing in the JSONPath says that filters should apply to objects. Sometimes workarounds can be found, see here or here.