我正在尝试使用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);
|>
}
Run Code Online (Sandbox Code Playgroud)
我期望的控制台输出是telefora对象,而是从json文件中获取所有三个对象.
如果不是商家=='Telefora'我使用merchantID == 16那么它的效果很好.我认为jsonPath也可以匹配字符串.虽然上面的例子没有搜索json的merchantDomain部分,但我遇到了同样的问题.
你的问题来自于一个事实,即在规定的文件,字符串相等运营商eq
,neq
以及like
.==
仅适用于数字.在您的情况下,您想要测试一个字符串是否等于另一个字符串,这是eq
字符串相等运算符的作用.
只是换==
了eq
你JSONpath过滤表达式,你会好到哪里去:
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 eq 'Telefora')]"); // replace == with eq
}
emit
<|
console.log(merchantData);
|>
}
Run Code Online (Sandbox Code Playgroud)
我把它放在我自己的测试规则集中进行测试,其来源如下:
ruleset a369x175 {
meta {
name "test-json-filtering"
description <<
>>
author "AKO"
logging on
}
dispatch {
domain "exampley.com"
}
global {
dataset merchant_dataset <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule filter_some_delicous_json {
select when pageview "exampley.com"
pre {
merchant_data = merchant_dataset.pick("$.merchants[?(@.merchant eq 'Telefora')]");
}
{
emit <|
try { console.log(merchant_data); } catch(e) { }
|>;
}
}
}
Run Code Online (Sandbox Code Playgroud)