Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

@property incorrectly excludes array index 0 because javascript treats 0 as false #245

Open
Labels
@Laavanja19

Description

Consider a sample JSON Document:

 {
 "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
 }
 }
}

Issue:
When using a filter operation with @property , JSONPath Plus incorrectly excludes array elements whose property (array index) is 0, because in JavaScript if(0) means it returns false.

Example JSONPath expression:

$.store.bicycle[?(@property)]

Output

red,
19.95

This works correctly because the children have string keys ("color" , "price"), which are truthy. Since each child has a property key , the filter operation for @property evaluate to true ,and therefore all child nodes are included.

But,

$.store.book[?(@property)]

Here, book is an array and then the @property for the array elements are going to be the array indices.

  • For the first element , @property is 0 (its array index)
  • For the second element, @property is 1 and so on.

But, here the problem is since for the first element array index is 0 , @property = 0, JavaScript evaluates if(0) as false. So, the first book element is incorrectly excluded. Only array elements at index 1,2,3 appear, because their properties are 1, 2, 3 , which are truthy. But the first array element should also be included since it also has a property.

JSONPath Expression

$.store.book[?(@property)]

Actual Output : Output lists except the first array element

 {
 "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
 }

Expected Output : Output should list all array elements
{
"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
}


Environment
- JSONPath-Plus version: jsonpath-plus@10.3.0
Desktop
 - OS: Ubuntu (Linux)
 - Node Version: v18.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /