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

continue to deal the result as a array after filter #202

Open
NingGelin opened this issue Jan 15, 2024 · 1 comment
Open

continue to deal the result as a array after filter #202

NingGelin opened this issue Jan 15, 2024 · 1 comment
Labels

Comments

@NingGelin
Copy link

NingGelin commented Jan 15, 2024

Motivation

I have a json like this:

{
  "contactMedium": [
    {
      "@type": "EmailContactMedium",
      "preferred": true,
      "contactType": "other",
      "validFor": {
        "startDateTime": "2018-10-22T08:31:52.028Z"
      },
      "emailAddress": "[email protected]"
    },
    {
      "@type": "GeographicAddressContactMedium",
      "preferred": true,
      "contactType": "other",
      "validFor": {
        "startDateTime": "2018-10-22T08:31:52.028Z"
      },
    }
  ]
}

I want to get the number of item with preferred==true
const result = JSONPath({ path: '$.contactMedium[?(@.preferred==true)].length', json: data })

Current behavior

got []

Desired behavior

got 2

Alternatives considered

currently, i can got the result expected by this way.
const result = JSONPath({ path: '$.contactMedium[?(@.preferred==true)]', json: data })
console.log(result)

const result2 = JSONPath({ path: '$.length', json: result })
console.log(result2)

@gerad
Copy link

gerad commented Oct 10, 2024

+1 to this issue. I think I can get my company to sponsor the fix (depending on the cost).

gerad added a commit to gerad/JSONPath that referenced this issue Oct 11, 2024
The length attribute works by happy accident today. This adds initial explicit
support for it and also gets it work with filter conditions.

This is not complete support. It won't (for instance) work correctly within
array filter conditions. See the test changes for use cases it does add support
for.

Note that the ietf standardization for length() and count() functions is done
differently.

See: https://ietf-wg-jsonpath.github.io/draft-ietf-jsonpath-base/draft-ietf-jsonpath-base.html#name-length-function-extension

Fixes JSONPath-Plus#202
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants