JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
958 stars 169 forks source link

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

Open NingGelin opened 7 months ago

NingGelin commented 7 months ago

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)