Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
con-f-use opened this issue Jun 4, 2024 · 3 comments
Open

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

con-f-use opened this issue Jun 4, 2024 · 3 comments

Comments

@con-f-use
Copy link

con-f-use commented Jun 4, 2024

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
Copy link

可能是语法不支持吧,分开步骤就可以得到结果
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
Copy link
Author

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
Copy link

all right

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants