1

I want to get the result without some property, can somebody know how to work on json.net? There is an example on the jPath "hello word" document in Goessner articles,

{ 
"store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

If I want to filter all books with isbn number, I can use jToken.SelectTokens("$..book[?(@.isbn)]"), however, if I want to filter all books without isbn number, how can I filter them and whether json.net has this feature.

luruibo
  • 11
  • 1
  • Well for JayWay's implementation you can do `$..book[?(!@.isbn)]` and it works. You can test it at https://jsonpath.com/. See the answer by [sprad](https://stackoverflow.com/users/5191504/sprad) to [JsonPath syntax for "does not include", or negative matching?](https://stackoverflow.com/q/42031746/3744182) for confirmation. – dbc Sep 10 '21 at 02:41
  • But `$..book[?(!@.isbn)]` doesn't work for Json.NET, see https://dotnetfiddle.net/37wKil. Checking the [Json.NET source code](https://github.com/JamesNK/Newtonsoft.Json/blob/52e257ee57899296d81a868b32300f0b3cfeacbe/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs#L16) it seems there is nothing like `QueryOperator.Not` so it seems your desired functionality is not implemented in Json.NET. – dbc Sep 10 '21 at 02:41
  • 1
    @dbc, yes, neither `$..book[?(!@.isbn)]` nor `$..book[?(@.isbn == null)]` works, I have created an issue on it, [link](https://github.com/JamesNK/Newtonsoft.Json/issues/2583) – luruibo Sep 10 '21 at 07:16
  • `$..book[?(@.isbn == null)]` would match `"isbn": null` which isn't what you want. `$..book[?(!@.isbn)]` which is implemented by other libraries is what you want but it seems not to be implemented. – dbc Sep 10 '21 at 07:24

1 Answers1

-1

Edit:

Sorry, this only works if the key is present with null, not if it's absent. Got mixed up.


For now, you can use $..book[?(@.isbn==null)].

The reason this works is related to another question I answered recently: https://stackoverflow.com/a/69069335/878701

gregsdennis
  • 7,218
  • 3
  • 38
  • 71
  • it's ok, I have also created an issue [link](https://github.com/JamesNK/Newtonsoft.Json/issues/2583) on it. Really thank you for your answer. – luruibo Sep 10 '21 at 07:13