json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.87k stars 1.64k forks source link

Getting the last element after filtering doesn't seem to work #1012

Open con-f-use opened 3 months ago

con-f-use commented 3 months ago

Given this json:

{
  "changes": [
    {
      "toHash": "0000000000000000000000000000000000000000"
    },
    {
      "toHash": "bb7550763f970f71a9053bf238ec46815c33c4e3"
    },
    {
      "toHash": "198fb09bb2aab83bf238ec53e01a48ac83c553d1",
      "WANTED": "WANTED"
    },
    {
      "toHash": "0000000000000000000000000000000000000000"
    }
  ]
}

I would expect this JSONPath expression:

$.changes.[?(@.toHash!="0000000000000000000000000000000000000000")][-1:]

To give the last element, where the toHash is not all zeros, i.e. element:

    {
      "toHash": "198fb09bb2aab83bf238ec53e01a48ac83c553d",
      "WANTED": "WANTED"
    }

However, I get an empy list []. Ist this a bug, or am I misunderstanding something?

admincopm commented 2 months ago

可能是语法不支持吧,分开步骤就可以得到结果 Configuration configuration = Configuration.builder() .jsonProvider(new JsonSmartJsonProvider()) .build(); // 解析一次文档 ReadContext ctx = JsonPath.using(configuration).parse(json); List<Map<String, Object>> filteredChanges = ctx.read("$.changes[?(@.toHash != '0000000000000000000000000000000000000000')]"); Map<String, Object> lastFilteredChange = filteredChanges.isEmpty() ? null : filteredChanges.get(filteredChanges.size() - 1); System.out.println(lastFilteredChange);

con-f-use commented 2 months ago

Thank you, but unfortunately I cannot separate the steps, since I'm using it in a Jenkins plugin. I have no influence over the Java code.

admincopm commented 2 months ago

all right