使用jsonPath查找字符串 [英] Using jsonPath looking for a string

查看:426
本文介绍了使用jsonPath查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jsonPath和pick函数来确定规则是否需要基于当前域运行.我正在做的事情的简化版本在这里:

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

我期望的控制台输出是telefora对象,相反,我从json文件中获得了所有三个对象.

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

如果我使用商人ID == 16而不是商人=='Telefora',那么它的效果很好.我认为jsonPath也可以匹配字符串.尽管上面的示例并未针对json的MerchantDomain部分进行搜索,但与此同时我也遇到了同样的问题.

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.

推荐答案

您的问题来自以下事实:如

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.

只需在您的JSONpath过滤器表达式中将==替换为eq,您就会很高兴:

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

这篇关于使用jsonPath查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆