I'm trying to query a JSON file with a JSONPath expression, executed with a
JObject.selectTonkens() call.
Unfortunately, I don't get the expected output.
I have tried many ways on https://jsonpath.com/ and with Newtonsoft.
These are the several attempts that do not give me the expected result :
IEnumerable<JToken> rawProperties = obj.SelectTokens("$.properties[?(@..type)]");
foreach (JToken token in rawProperties)
{
Console.WriteLine(token);
}
This does give me every property (including the ones I don't want) that do have a type. I've tried it in visual studio.
Best attempt :
$.properties[?(@.type != 'array')]
I've tried it on https://jsonpath.com/ , unfortunately, it does not work at all on Visual Studio. It gives me the good expected result but without the keys I need (cf expected output at the end of my post) :
[
{
"type": "string",
"description": "Identifiant technique de l'action de régulation."
},
{
"type": "string",
"description": "Description de l'action de régulation."
},
{
"type": "string",
"description": "situations/id : identifiant technique de la situation."
}
]
This is a sample of the JSON file I'm trying to query :
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "actionRegulation",
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Identifiant technique de l'action de régulation."
},
"description": {
"type": "string",
"description": "Description de l'action de régulation."
},
"idSituation": {
"type": "string",
"description": "situations/id : identifiant technique de la situation."
},
"pointsJalonnement": {
"type": "array",
"items": {
"title": "PointJalonnement",
"type": "object",
"properties": {
"rang": {
"type": "integer",
"description": "Rang du point de jalonnement."
},
"refJalon": {
"type": "string",
"description": "Référence du point de jalonnement (PR/CR-CI-CH)."
},
"typeModification": {
"type": "string",
"description": "Renseigné que si modification d'horaire (utile à IENA)."
}
}
}
}
}
}
I want to retrieve all the properties (including their name, like "id", "description") but that are not a "type":"array" and that do not belong to an "array" type object, that is to say that are inner properties. I for example do not want to retrieve from my query "refJalon", "typeModification", etc. So I expect to have the following result from my query :
[
"id":
{
"type": "string",
"description": "Identifiant technique de l'action de régulation."
},
"description":
{
"type": "string",
"description": "Description de l'action de régulation."
},
"idSituation":
{
"type": "string",
"description": "situations/id : identifiant technique de la situation."
}
]
Any piece of help would be really appreciated : other technologies/frameworks I should use, some specific documenation, etc. Thanks!
You may not be able to get the result you want using JsonPath; there are limitations as @dbc mentioned in the comments. However, you can get the results you want using a pretty straightforward LINQ-to-JSON query:
IEnumerable<JToken> rawProperties = obj["properties"]
.Children<JProperty>()
.Where(jp => (string)jp.Value["type"] != "array");
foreach (JToken token in rawProperties)
{
Console.WriteLine(token);
}
Fiddle: https://dotnetfiddle.net/A4ZbEG
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