使用jsonPath查找字符串

编程入门 行业动态 更新时间:2024-10-11 17:28:41
本文介绍了使用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 <- "s3.amazonaws/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.

推荐答案

您的问题来自以下事实:如文档,则字符串相等运算符为eq,neq和like. ==仅适用于数字.在您的情况下,您想测试一个字符串是否等于另一个字符串,这是eq字符串相等运算符的工作.

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 <- "s3.amazonaws/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" } global { dataset merchant_dataset <- "s3.amazonaws/app-files/dev/merchantJson.json" cachable for 2 seconds } rule filter_some_delicous_json { select when pageview "exampley" pre { merchant_data = merchant_dataset.pick("$.merchants[?(@.merchant eq 'Telefora')]"); } { emit <| try { console.log(merchant_data); } catch(e) { } |>; } } }

更多推荐

使用jsonPath查找字符串

本文发布于:2023-11-30 13:15:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   jsonPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!