2011-04-06 37 views
4

我想使用jsonPath和pick功能来确定规则是否需要运行或不是基于当前域。我在做什么的简化版本在这里:使用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=='Telefora')]"); 
    } 
    emit 
    <| 
     console.log(merchantData); 
    |> 
} 

控制台输出我希望是telefora对象,而不是我从JSON文件中的所有三个对象。

如果不是商家=='Telefora'我使用merchantID == 16,那么它的效果很好。我认为jsonPath也可以对字符串进行匹配。虽然上面的例子不是针对json的merchantDomain部分进行搜索,但我遇到了同样的问题。

回答

5

您的问题来自如下事实:如the documentation所述,字符串相等运算符为eq,neqlike==仅适用于数字。在你的情况下,你想测试一个字符串是否等于另一个字符串,这是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); 
    |> 
} 

我把这个测试在我自己的测试规则集的来源,低于:

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) { } 
     |>; 
    } 
    } 
} 
+0

感谢您解释'eq'和'=='之间的区别。那一个让462名学生(和其他新人)感到困惑。 – 2011-04-06 03:40:59

+0

不错!我感到困惑,因为我正在查看实际的jsonPath文档,看看我是否可以让这些工作。谢谢Alex! – trumans1 2011-04-06 14:42:20

+0

像这样的情况,运营商会怎么看?我无法使正则表达式正常工作。 – trumans1 2011-04-06 15:18:24