2011-04-06 128 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