-4

I have a json below

"root":[
  {
    "refDataId": 1,
    "children": [
      {
        "refDataId": 20
      },
      {
        "refDataId": 99,
        "otherValue": "Facility"
      }
    ]
  },
  {
    "refDataId": 2,
    "children": [
      {
        "refDataId": 30
      },
      {
        "refDataId": 99,
        "otherValue": "Officer"
      }
    ]
  }
]

How to check the value above using the if statement in rule drools?

I edited the question. This is for drools rule

For example my rules is:

rule "test"
    when
        RuleEngine(inputObject!.adultHealth!.children contains 99)
    then
        info("children contains value 99");
end

And how to check the value of "refDataId": 99, and "otherValue": "Officer"?

If the value has to get from 2nd child json "refDataId": 2,

  • 1
    What did you try so far? I can't see any if-statement so please share your code and what problems you're facing exactly. Besides that please take a [tour] and read [ask]. – Thomas Aug 06 '18 at 06:42

2 Answers2

1

You can extract data using any json parser I have used org.json

The following code tries to find an OFFICER from your json data.

JSONObject obj = new JSONObject(jsonString);

    JSONArray objArray = obj.optJSONArray("root");

    for (Object jo : objArray) {
        JSONObject arrayElement = new JSONObject(jo.toString());
        JSONArray childrenArray = arrayElement.getJSONArray("children");

        for (Object child : childrenArray) {

            JSONObject childJo = new JSONObject(child.toString());

            if (Integer.parseInt(childJo.get("refDataId").toString()) == 99) {
                if (childJo.get("otherValue").toString().equals("Officer")) {
                    System.out.println("Success Officer Found !");
                }
            }

        }

    }
Sanket9394
  • 2,031
  • 1
  • 10
  • 15
0

Looking here Nested JSON iterations using drools Fluent API it seems to me it is enough to create data objects corresponding to structure of your JSON.

jomarko
  • 127
  • 3