4

I'm writing a JSONPath expression that counts objects in an array while filtering by one of the field of the object.

Input JSON taken from http://jsonpath.herokuapp.com/

{
    "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
        }
    },
    "expensive": 10
}

And my expression that extracts object where "category" is "fiction".

$.store.book[?(@.category == "fiction")]

The output is as expected.

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

So far so good. What I would like to achieve is to count the number of objects in the array. In this case 3. I tried this expression:

$.store.book[?(@.category == "fiction")].length()

The output is not what I expected.

[
   4,
   5,
   5
]

The below didn't work either

$.store[?(@.book[*].category == "fiction")].length()

How do I count the number of elements in the array? If I use jq it could be written as

[.store.book[] | select (.category == "fiction")] | length
5
  • 1
    Looks like I have to wrap the resulting object in another JsonPath , and apply the length(). JsonPath appears to be pretty weak Commented Apr 1, 2017 at 2:52
  • 2
    This is a weird thing that always seemed as a result of wrong design concept to me. Quite annoying. Commented Apr 1, 2017 at 14:05
  • Indeed the API seems to have some design flaw. I started looking into jq for Java. It's a JNA wrapper for libjq. If I can make it work with Jackson JSON object, I will switch to jjq Commented Apr 2, 2017 at 1:59
  • What if you just filter the nodes and evaluate the expression to JSONArray so that just take it length? It does not seem to be very declarative, but it does not require "heavy weapons" like what JNA is. Commented Apr 2, 2017 at 5:50
  • I ditched the JsonPath. I've been using jackson-jq. It's mostly compatible with jq. jjq is still a viable option if you need the genuine jq. Commented Apr 25, 2020 at 20:46

1 Answer 1

2

Very late for the answer but I came across this scenario recently and managed to get the number of object inside the array using below JsonPath query as per the JSON provided in question:

$.length($.store.book[?(@.category == "fiction")].length())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.