0

newtonsoft version 13.0.1

given the object data which is a JToken with the content of:

{
  "myString": "demo2",
  "myNumber": 2.2,
  "myInteger": 20,
  "myObject": {
    "myObject": {
      "myArray": [
        2,
        20,
        200,
        2000
      ]
    },
    "myArray": [
      2,
      20,
      200,
      2000
    ]
  },
  "myArray": [
    2,
    20,
    200,
    2000
  ],
  "myBoolean": true,
  "myNull": null
}

and the selecttokens command like this.

data.SelectTokens("$..myObject[?(@.myArray)]")

I expect to get one object back. but it doesn't return any. Is there something I do wrong here?

if I try it on https://jsonpath.com/ it will return me an object.

or should I test it like $..[?(@.myObject.myArray)]

dbc
  • 104,963
  • 20
  • 228
  • 340
Frans
  • 131
  • 1
  • 10
  • 1
    `SelectTokens()` only supports filter expressions for array contents, not object contents. See [json path expression not working without array](https://stackoverflow.com/a/51548560/3744182). But I think `..[?(@.myObject.myArray)]` might work, see [Json.NET JSONPath query not returning expected results](https://stackoverflow.com/a/45298348/3744182) for a similar question. – dbc Aug 25 '21 at 15:46
  • Not sure how the expected result should look like but this should only return one item: `$.myObject.myObject[?(@.myArray)]` – wp78de Aug 26 '21 at 01:23
  • @dbc thanks for the reply and the links to clarify the issue. Unfortunately, it is just an interpretation of the Jsonpath filtering, like explained in the links. It holds some valuable solutions/ workarounds. – Frans Aug 26 '21 at 18:13
  • @wp78de, although your suggestion works it is limited to the structure I provided. The intention was to look for all objects that hold the myArray property. – Frans Aug 26 '21 at 18:13
  • 1
    So, you want an array with three myarray elements and sub-nodes? You may want to outline the expected result more clearly. – wp78de Aug 26 '21 at 20:07

0 Answers0