【问题标题】:Using jsonPath looking for a string使用 jsonPath 查找字符串
【发布时间】:2011-04-06 01:16:18
【问题描述】:

我正在尝试使用 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 文件中获取所有三个对象。

如果我使用 MercerID==16 而不是商人=='Telefora',那么效果很好。我认为 jsonPath 也可以匹配字符串。虽然上面的示例没有搜索 json 的 MercerDomain 部分,但我遇到了同样的问题。

【问题讨论】:

    标签: jsonpath krl


    【解决方案1】:

    您的问题来自这样一个事实,如the documentation 中所述,字符串相等运算符是eqneqlike== 仅用于数字。在您的情况下,您想测试一个字符串是否等于另一个字符串,这是 eq 字符串相等运算符的工作。

    只需在 JSONpath 过滤器表达式中将 == 替换为 eq 即可:

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

    【讨论】:

    • 感谢您解释eq== 之间的区别。这让 462 名学生(和其他新人)感到困惑。
    • 不错!我很困惑,因为我正在查看实际的 jsonPath 文档并查看是否可以让它们工作。谢谢亚历克斯!
    • 在这种情况下,like 运算符会如何?我无法让正则表达式正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多