验证一些键的值是否在有效负载中。

huangapple go评论77阅读模式
英文:

verify if the value of some keys are in the payload

问题

{
"money": 0.22,
"perMinute": null,
"perSession": null
}

英文:

I have this input:

  1. [
  2. {
  3. "building": 222,
  4. "default": {
  5. "money": 0.22,
  6. "perHour": null
  7. },
  8. "appPricing": [
  9. {
  10. "pricing": {
  11. "money": 1.25,
  12. "perHour": null
  13. },
  14. "schedules": [
  15. {
  16. "daysOfWeek": [
  17. "FRIDAY",
  18. "MONDAY"
  19. ],
  20. "end": "21:59:00",
  21. "start": "08:00:00"
  22. },
  23. {
  24. "days": [
  25. "TUESDAY",
  26. "WEDNESDAY",
  27. "THURSDAY"
  28. ],
  29. "end": "15:59:00",
  30. "start": "11:00:00"
  31. }
  32. ]
  33. }
  34. ]
  35. }
  36. ]

And I want to filter after a specific day, a specific end time, and a start time.
Here is the output:

  1. {
  2. "money": 0.22,
  3. "perMinute": null,
  4. "perSession": null
  5. }

Thanks And I want to filter after a specific day, a specific end time, and a start time.

答案1

得分: 0

我假设你正试图根据时间表筛选定价,并在找不到定价时返回默认定价。关键是内部筛选器返回布尔条件,我已使用isEmpty!运算符进行了相同处理。

然而,我看到输出与输入之间存在差异。例如,输出中的perMinute和perSession字段。因此,请使用以下脚本作为起点,根据您的要求进行扩展/修改脚本。

希望这有所帮助!

英文:

I assume you are trying to filter pricing based on schedules and return default pricing in case no pricing found. The key is the internal filter to return the boolean condition, I have used isEmpty with ! operator for the same.

However, I see there is a difference in the output vs the input. For example, perMinute and perSession fields in output. So please use the below script as starting point, and extend/modify the script according to your requirements.

Hope this helps!

  1. %dw 2.0
  2. output application/json
  3. var inp = {
  4. day: "THURSDAY",
  5. endTime: "16:59:00",
  6. startTime: "11:00:00"
  7. }
  8. fun matchSchedules (schedules) =
  9. !isEmpty (schedules filter (($.daysOfWeek contains inp.day)
  10. and $.endTime == inp.endTime
  11. and $.startTime == inp.startTime))
  12. ---
  13. ((payload[0].appPricing filter ((price) -> matchSchedules(price.schedules)))[0].pricing) default payload[0]."default"

huangapple
  • 本文由 发表于 2023年8月10日 17:25:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定