使用 jsonPath 查找字符串

发布于 2024-10-30 06:59:46 字数 662 浏览 2 评论 0原文

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

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

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

The console output I expect is the telefora object, instead I get all three objects from the json file.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2024-11-06 06:59:46

您的问题来自这样一个事实:正如 文档 中所述,字符串相等运算符是 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) { }
        |>;
    }
  }
}

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.

Simply swap == for eq in you JSONpath filter expression and you will be good to go:

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

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.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) { }
        |>;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文