Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eval length() function returns wrong answer after filtering array #67

Closed
Joey29-1 opened this issue Jun 13, 2024 · 7 comments · Fixed by #68
Closed

Eval length() function returns wrong answer after filtering array #67

Joey29-1 opened this issue Jun 13, 2024 · 7 comments · Fixed by #68

Comments

@Joey29-1
Copy link

Joey29-1 commented Jun 13, 2024

The length() function in Eval() does not return the correct answer if the array of objects is filtered.

In the code snippet below, there is no element in $.items that matches the array filtering condition, but the length of the resulting array is still evaluated to 1 instead of 0.

json := []byte(`
{
  "items": [
    {
      "price":1, 
      "type": "A"
    }, 
    {
      "price":2, 
      "type": "B"
    }, 
    {
      "price":3, 
      "type": "C"
    }
  ]
}`)

root, err := ajson.Unmarshal(json)
if err != nil {
  panic(err)
}
res1, err := ajson.Eval(root, `length($.items[?(@.type == "D")])`)
if err != nil {
  panic(err)
}
fmt.Printf("Result: %+v", res1.MustNumeric()) // Expected: 0, Actual: 1

res2, err := ajson.Eval(root, `length($.items[?(@.type == "A")])`)
if err != nil {
  panic(err)
}
fmt.Printf("Result: %+v", res2.MustNumeric()) // Expected: 1, Actual: 2

res3, err := ajson.Eval(root, `length($.items[?(@.type == "A" || @.type == "B")])`)
if err != nil {
  panic(err)
}
fmt.Printf("Result: %+v", res3.MustNumeric()) // Expected: 2, Actual: 2
@Joey29-1
Copy link
Author

What I have figured out after tracing the code:

  • If none of the elements match the array filter condition, the resultant Node has NodeType Null. According to the implementation of the length() function, a value of 1 is returned.
  • On a similar vein, if only 1 element matches the filter condition, the resultant Node has NodeType Object (I thought it was Array). The length() function then returns node.Size().

@spyzhov
Copy link
Owner

spyzhov commented Jun 13, 2024

Thank you for sharing this! I will try to prepare the fix today ASAP.

spyzhov added a commit that referenced this issue Jun 13, 2024
…size` function to provide the count of internal elements of arrays and objects.
spyzhov added a commit that referenced this issue Jun 13, 2024
#68)

* #67 `length` returns the size of array or length of the string. Add `size` function to provide the count of internal elements of arrays and objects.
@spyzhov
Copy link
Owner

spyzhov commented Jun 13, 2024

Fixed in v0.9.2

@tpoxa
Copy link

tpoxa commented Oct 20, 2024

BTW why the length of null is 1? @spyzhov
How can I check if an array has elements while array itself could be null?

@spyzhov
Copy link
Owner

spyzhov commented Oct 20, 2024

Good point... length of the element is equal to 1, for the reason that the value that you select is always the array, as soon as any JSONPath response is array.

What you are asking about... I suppose I should add some new functions, like is_*type*. I will try to add it asap, or feel free to suggest changes :-)

spyzhov added a commit that referenced this issue Oct 22, 2024
spyzhov added a commit that referenced this issue Oct 22, 2024
@spyzhov
Copy link
Owner

spyzhov commented Oct 22, 2024

@tpoxa, I added is_*type* functions in v0.9.5

@tpoxa
Copy link

tpoxa commented Oct 25, 2024

thanks @spyzhov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants