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

Is there any operator for checking the nested JSON facts. #62

Closed
skadthan opened this issue Dec 7, 2017 · 1 comment
Closed

Is there any operator for checking the nested JSON facts. #62

skadthan opened this issue Dec 7, 2017 · 1 comment
Labels

Comments

@skadthan
Copy link

skadthan commented Dec 7, 2017

Let us say, we have the following JSON object. It is like looping through the nested JSON payload and looking for a value match. Here in the below example, indexes being hard-coded, which I wanted to avoid.

JSON Fact Payload :

{
"ObjectA": {
"ObjectA1": {
"dt": "2017-01-10T16:45:20.071Z",
"ObjectA1Array": [
{
"var1": "value1",
"ObjectA1ArrayObject1": {"var2": "value2"}
"ObjectA1ArrayObject2": {
"ObjectA1ArrayObject2Array": [
{
"var3": "value3",
"ObjectA1ArrayObject2ArrayObject1": {"var4": "value4"}
},
{
"var4": "value4",
"ObjectA1ArrayObject2ArrayObject2": {"var5": "value5"}
}
]
}
},
{
"var6": "value6",
"ObjectA1ArrayObject3": {"var7": "value"}
}
]
}
}
}

let exampleRule = {
conditions: {
all: [{
fact: 'JSONPayLoad',
operator: 'equal',
value: 'value1',
path: '.ObjectA.ObjectA1.ObjectA1Array[0].var1'
}, {
fact: 'JSONPayLoad',
operator: 'in',
value: ['value5', 'value10'],
path: '.ObjectA.ObjectA1.ObjectA1Array[1].var5'
}, {
fact: 'JSONPayLoad',
operator: 'greaterThanInclusive',
value: 'value5',
path: '.ObjectA.ObjectA1.ObjectA1Array[0].ObjectA1ArrayObject2.ObjectA1ArrayObject2Array[1].ObjectA1ArrayObject2ArrayObject2.var5'
},
event: {
type: 'something',
params: {
message: 'Some message'
}
},
priority: 100
}

@CacheControl
Copy link
Owner

CacheControl commented Dec 8, 2017

@skadthan The path feature is provided by selectn under the hood, which as far as I know is the most advanced library around for accessing object paths via string. To my knowledge it doesn't provide the functionality you're looking here, but you may want to take a look in case I'm misremembering.

In terms of solving this for yourself, you could experiment with building a dynamic fact which would give you greater flexibility to traverse the data structure. This is uncharted territory so I can't provide much guidance, but you might be able to get creative.

Finally, make sure the fact data you are feeding into the engine is structured properly - that can certainly make things easier from an access standpoint. So for example, it might make sense to add every element in ObjectA1Array into the engine as its own fact, rather than loading an entire tree in as a single fact and attempting a complex traversal via path. Or taking that a step further, maybe you do some preprocessing to recursively load all the elements contained in the object as an individual fact. Depending on your data and rules this may not be an option, but thought I'd mention it.

In general, the path feature is meant for traversing simple objects, which helps DRY up repetitive rule definitions. Accessing very complex data structures could get tricky to maintain over time, since if the data structure changes in any way, your rules may cease to work. My advice is to find a way to make your facts be simple objects with minimal nesting.

Hope that helps!

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

No branches or pull requests

2 participants