4

I am trying to filter books in my json that does not have "name" property.

I found it is supported in Java JsonPath

$..book[?(!@.name)]

But when I use it in C# ::

json.SelectTokens("$..book[?(!@.name)]")

I got ::

"Unexpected character while parsing path query: !"

Also tried ::

json.SelectTokens("$..book[?(@.name == null)]")

Which is parsed but 0 results returned.

user1316502
  • 589
  • 2
  • 6
  • 19

1 Answers1

0

You can use LINQ syntax to get tokens that do not contain a property.

I don't know your exact JSON so I have mocked a simple example. If you post your JSON I will update my answer to work with your specific JSON.

Test JSON:

JObject o = JObject.Parse(@"{
        'books': [
            {'book':{'name':'some book name 1', 'author':'bob smith'}},
            {'book':{'author':'mary smith'}}
            ]}");

How to get tokens without name property:

var result = o.SelectTokens("books[*].book").Where(t => t["name"] == null);

// result = {{  "author": "mary smith" }}
quaabaam
  • 1,248
  • 1
  • 4
  • 13