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 · 3 comments
Open

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

NingGelin opened this issue Jan 15, 2024 · 3 comments
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": "tom@email.com"
    },
    {
      "@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
@80avin
Copy link
Collaborator

80avin commented Feb 14, 2025

@NingGelin I am not able to understand the use-case.

Why can't we use something like

const data = {
  "contactMedium": [
    {
      "@type": "EmailContactMedium",
      "preferred": true,
      "contactType": "other",
      "validFor": {
        "startDateTime": "2018-10-22T08:31:52.028Z"
      },
      "emailAddress": "tom@email.com"
    },
    {
      "@type": "GeographicAddressContactMedium",
      "preferred": true,
      "contactType": "other",
      "validFor": {
        "startDateTime": "2018-10-22T08:31:52.028Z"
      },
    }
  ]
}
const result = JSONPath({ path: '$.contactMedium[?(@.preferred==true)]', json: data }).length

@brettz9
Copy link
Collaborator

brettz9 commented Feb 14, 2025

I think the use case would be for applications which exposed JSONPath expressions to users, and to allow the users to make a query which performed a count.

We should probably not be making custom additions to the JSONPath standard now though, so I would discuss any new proposals (e.g., for a count function) with the standards-making body if there is not already one in effect.

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.

4 participants