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

Filter out all keys with a specific name from all objects #294

Open
roshan-joyce-fujitsu opened this issue May 22, 2023 · 1 comment
Open
Labels
support Users asking how to solve a specific issue

Comments

@roshan-joyce-fujitsu
Copy link

Is it possible to write a filter that checks all the key names and filter out those with specific names.
For example, if the input is

{
  "id": 1,
  "name": "n1",
  "_parent": 0,
  "children" : [
    {
      "id": 11,
      "name": "n11",
      "_parent": 1,
      "children": [
        {
          "id": 111,
          "name": "n111",
          "_parent": 11
        }
      ]
    }
  ]
}

I want the output not to have "_parent" attribute. So, the output should be:

{
  "id": 1,
  "name": "n1",
  "children" : [
    {
      "id": 11,
      "name": "n11",
      "children": [
        {
          "id": 111,
          "name": "n111",
        }
      ]
    }
  ]
}

Doing the following is not working.

String filter = " .key != \"_parent\" ";
StringReader jslt = new StringReader(".");
Expression expr = new Parser(jslt).withObjectFilter(filter).compile();

return expr.apply(resultObj);
@larsga larsga added the support Users asking how to solve a specific issue label May 22, 2023
@larsga
Copy link
Collaborator

larsga commented May 22, 2023

You can write a function which goes through the values recursively, and use

{for (.) .key : .value if (.key != "_parent")}

to do the filtering at each step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Users asking how to solve a specific issue
Projects
None yet
Development

No branches or pull requests

2 participants