How to filter list of data in list in java with sql or nested dynamic condition

How to filter data dynamically using java, Assume we have data (list of map / json array without pojo mapping).

[
  {
    "id": "1001",
    "type": "Internal",
    "status": "Closed"
  },
  {
    "id": "1002",
    "type": "External",
    "status": "Closed"
  },
  {
    "id": "1003",
    "type": "Internal",
    "status": "Open"
  },
  {
    "id": "1004",
    "type": "Internal",
    "status": "Open"
  }
]

Now we need out put filtered data as id > 1001 and ( type: 'External' or status: 'Open" )

 [ 
  {
    "id": "1002",
    "type": "External",
    "status": "Closed"
  },
  {
    "id": "1003",
    "type": "Internal",
    "status": "Open"
  },
  {
    "id": "1004",
    "type": "Internal",
    "status": "Open"
  } 
]

Any Suggestions how to achieve this ?

Answer

We can use javascript ability to resolve condition. We can use Java ScriptEngineManager to run javascript expression : Expression can be type = 'Internal' AND status ='Open'

    private String getResult(String expression) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        try {
            return engine.eval(expression).toString();
        } catch (Exception e) {
        }
        return null;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *