使用 jsonPath 查找字符串
我正在尝试使用 jsonPath 和 pick 函数来确定规则是否需要基于当前域运行。我正在做的事情的简化版本如下:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(@.merchant=='Telefora')]");
}
emit
<|
console.log(merchantData);
|>
}
我期望的控制台输出是 telefora 对象,而不是从 json 文件中获取所有三个对象。
如果我使用merchantID==16而不是merchant=='Telefora',那么效果很好。我认为 jsonPath 也可以对字符串进行匹配。虽然上面的示例没有搜索 json 的merchantDomain 部分,但我遇到了同样的问题。
I'm trying to use jsonPath and the pick function to determine if a rule needs to run or not based on the current domain. A simplified version of what I'm doing is here:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(@.merchant=='Telefora')]");
}
emit
<|
console.log(merchantData);
|>
}
The console output I expect is the telefora object, instead I get all three objects from the json file.
If instead of merchant=='Telefora' I use merchantID==16 then it works great. I thought jsonPath could do matches to strings as well. Although the example above isn't searching against the merchantDomain part of the json, I'm experiencing the same problem with that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题来自这样一个事实:正如 文档 中所述,字符串相等运算符是
eq
、neq
和like
。==
仅适用于数字。在您的情况下,您想要测试一个字符串是否等于另一个字符串,这是 eq 字符串相等运算符的工作。只需在 JSONpath 过滤器表达式中将
==
替换为eq
即可:我将其放入我自己的测试规则集中进行测试,其来源是以下:
Your problem comes from the fact that, as stated in the documentation, the string equality operators are
eq
,neq
, andlike
.==
is only for numbers. In your case, you want to test if one string is equal to another string, which is the job of theeq
string equality operator.Simply swap
==
foreq
in you JSONpath filter expression and you will be good to go:I put this to the test in my own test ruleset, the source for which is below: