我正在尝试使用jsonPath和pick函数来确定规则是否需要基于当前域运行.我正在做的事情的简化版本在这里:
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 <- "s3.amazonaws/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文件中获得了所有三个对象.
The console output I expect is the telefora object, instead I get all three objects from the json file.
如果我使用商人ID == 16而不是商人=='Telefora',那么它的效果很好.我认为jsonPath也可以匹配字符串.尽管上面的示例并未针对json的MerchantDomain部分进行搜索,但与此同时我也遇到了同样的问题.
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.
推荐答案您的问题来自以下事实:如文档,则字符串相等运算符为eq,neq和like. ==仅适用于数字.在您的情况下,您想测试一个字符串是否等于另一个字符串,这是eq字符串相等运算符的工作.
Your problem comes from the fact that, as stated in the documentation, the string equality operators are eq, neq, and like. == is only for numbers. In your case, you want to test if one string is equal to another string, which is the job of the eq string equality operator.
只需在您的JSONpath过滤器表达式中将==替换为eq,您就会很高兴:
Simply swap == for eq in you JSONpath filter expression and you will be good to go:
global { dataset shopscotchMerchants <- "s3.amazonaws/app-files/dev/merchantJson.json" cachable for 2 seconds } rule checkdataset is active { select when pageview ".*" setting () pre { merchantData = shopscotchMerchants.pick("$.merchants[?(@.merchant eq 'Telefora')]"); // replace == with eq } emit <| console.log(merchantData); |> }我将其放在自己的测试规则集中进行测试,其来源如下:
I put this to the test in my own test ruleset, the source for which is below:
ruleset a369x175 { meta { name "test-json-filtering" description << >> author "AKO" logging on } dispatch { domain "exampley" } global { dataset merchant_dataset <- "s3.amazonaws/app-files/dev/merchantJson.json" cachable for 2 seconds } rule filter_some_delicous_json { select when pageview "exampley" pre { merchant_data = merchant_dataset.pick("$.merchants[?(@.merchant eq 'Telefora')]"); } { emit <| try { console.log(merchant_data); } catch(e) { } |>; } } }更多推荐
使用jsonPath查找字符串
发布评论