0

I am working with jquery based queryBuilder and created a query JSON String, which is then passed to a Spring-Boot Service class. There I have a JSON Payload which contains numerous details along with the keys and values, I selected through QueryBuilder. My Use case is to identify that the queryBuilder string is matching in the payload json or not.

My Payload JSON file

    {
      "_id": {
        " $oid": "<hexa-decimal-id>"
      },
      "key1": {
        "$number Long": "240056"
      },
      "version": {
        " $numberLong": "35"
      },
      "key2": {..},
      "key3": {},
      "key4": {},
      "key5": {..},
      "Some Other Keys" :{..},
      "key10": [
        {
          "SubKey1": "TestValue1",
          "SubKey1DisplayName": "TestValue1 - Display",
          "SubKey2": "TestValue1-Key2",
          "SubKey3": false,
          "SubKey4": [
            {
              "InnerKey1": "With Referral",
              "InnerKey2": "Some String value",
              "InnerKey3": [
                {
                  "InnerMostKey1": {
                    " $numberLong": "2"
                  },
                  "InnerMostKey2": 0,
                  "InnerMostKey3": false,
                  "InnerMostKey4": false,
                  "InnerMostKey5": true,
                  "InnerMostKey6": "Testing",
                  "InnerMostKey7": false
                }
              ]
            }
          ]
        },
        {
          "SubKey1": "TestValue2",
          "SubKey1DisplayName": "Some Name",
          "SubKey2": "Some String",
          "SubKey3": false,
          "SubKey4": [
            {
              "InnerKey1": "Standard",
              "InnerKey2": "Some Longer String",
              "InnerKey3": [
                {
                  "InnerMostKey1": {
                    "$number Long": "2"
                  },
                  "InnerMostKey2": 0,
                  "InnerMostKey3": true,
                  "InnerMostKey4": false,
                  "InnerMostKey5": true,
                  "InnerMostKey6": "Testing*",
                  "InnerMostKey7": false
                }
              ]
            }
          ]
        },
        {
          "SubKey1": "TestValue3",
          "SubKey1DisplayName": "TestValue3 - Name",
          "SubKey2": "Other String",
          "SubKey3": false,
          "SubKey4": [
            {
              "InnerKey1": "Standard",
              "InnerKey3": [
                {
                  "InnerMostKey1": {
                    " $numberLong": "2"
                  },
                  "InnerMostKey2": 0,
                  "InnerMostKey3": false,
                  "InnerMostKey4": false,
                  "InnerMostKey5": true,
                  "InnerMostKey6": "No Testing",
                  "InnerMostKey7": false,
                  "InnerMostKey8" : 20
                }
              ]
            }
          ]
        }
      ]
    }

And the Query Builder JSON String

{
  "condition": "AND",
  "rules": [
    {
      "id": "SubKey1",
      "field": "SubKey1",
      "type": "string",
      "input": "text",
      "operator": "is equal to",
      "value": "TestValue2"
    },
    {
      "condition": "AND",
      "rules": [
        {
          "id": "InnerKey1",
          "field": "InnerKey1",
          "type": "string",
          "input": "text",
          "operator": "is equal to",
          "value": "Standard"
        },
        {
          "condition": "AND",
          "rules": [
            {
              "id": "InnerMostKey1",
              "field": "InnerMostKey1",
              "type": "integer",
              "input": "number",
              "operator": "is equal to",
              "value": 2
            },
            {
              "id": "InnerMostKey8",
              "field": "InnerMostKey8",
              "type": "integer",
              "input": "number",
              "operator": "is present",
              "value": null
            }
          ]
        }
      ]
    }
  ],
  "valid": true
}

The Code I used to find the field in payload json is selecting all the fields of same name in a collection and then compared values:

for (RulesModel rulesModel : rules) {
            boolean isValid = false;
            String field = rulesModel.getField().replaceAll("\\s", "");
            String jsonpathCreatorLocationPath = "$.." + field;
            String fieldValue = String.valueOf(rulesModel.getValue());
            DocumentContext jsonContext = JsonPath.parse(payload);
            List<String> jsonpathCreatorLocations = jsonContext.read(jsonpathCreatorLocationPath);
            for (Object jsonpathCreatorLocation : jsonpathCreatorLocations) {
                if (String.valueOf(jsonpathCreatorLocation).equalsIgnoreCase(fieldValue)) {
                    isValid = true;
                    break;
                }
            }
// capturing the isValid value in a collection
}

Where RulesModel class (Mapping the queryBuilder JSON into it)

@Getter
public class RulesModel {
    private String id;
    private String field;
    private String type;
    private String input;
    private String operator;
    private Object value;
    private String condition;
    private List<RulesModel> rules;
}

I want to validate if topmost condition from queryBuilder is matching then the nested conditions should be matched against the matched payload object only. and if all conditions are matching within same object in payload then isValid =true.

manu
  • 1
  • 1
  • I have no idea what you are asking here. Can you be more specific? Also, you have tagged the question with quite a spread of technology tags. Which specific technology do you need help with and what specific help do you need? You can also take the [tour] and read [ask] for more general guidance. – andrewJames May 04 '22 at 18:23
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 04 '22 at 20:06

0 Answers0