Search code examples
javascriptnode.jsjsonjsonpathjsonpath-plus

How to find object without existing property with JSONPath


I have the following json :

{
  "data": [
     {
       "name": "Peter",
       "excluder": 1
     }, 
     {
       "name": "Sansa"
     }
  ]
}

I want to obtain only elements without excluder property, using JSONPath or extended package jsonpath-plus :

[
  {
    "name": "Sansa"
  }
]

I know I can filter on elements with property excluder with $.data[?(@.excluder)]

Is it possible to filter on the non existance of a property ?


Solution

  • I know I can filter on elements with property excluder with $.data[?(@.excluder)]

    • ?() mean this contain a boolean expression, filtering on if it is true.
    • @.excluder is the boolean expression content, filtering on if excluder exists.

    To filter on elements without existing property excluder, which is the negation of filter elements with, have to put negation of current boolean expression.

    To put negation of the current boolean expression, just put a ! before the expression @.excluder :

    $.data[?([email protected])]
    

    Thanks to cmgchess for the comment giving me the solution.