json - Jolt Transformation Spec 中的值相乘

标签 json transformation jolt

我的输入如下所示,如果 timeMeasurementUnitCode="DAY" 那么我需要计算的值为 value*24*60,如果 timeMeasurementUnitCode="HOUR" 然后值 value*60

我看到人们正在寻找一个类似的用例,票证于 2019 年 7 月 17 日开立:https://github.com/bazaarvoice/jolt/issues/832

问题832现在好像还开着

输入:

{
  "freightCharacteristics": {
    "transitDuration": {
      "timeMeasurementUnitCode": "DAY",
      "value": 6
    },
    "loadingDuration": {
      "timeMeasurementUnitCode": "HOUR",
      "value": 6
    }
  }
}

预期输出:根据输入中提供的DAYHOUR常量,我需要多个no.of days*hours*mins code> 达到我的要求,如下所示。

{
  "transitDurationInMinutes" : 8640,
  "loadingDurationInMinutes" : 360
}

]

根据@mattyb的建议,我使用除法函数来实现乘法想要实现的目标,逻辑更新如下。

[
  {
    "operation": "shift",
    "spec": {
      "freightCharacteristics": {
        "transitDuration": {
          "timeMeasurementUnitCode": {
            "DAY": {
              "@(2,value)": "transitDurationInMinutes"
            },
            "*": {
              "@(2,value)": "transitDurationInMinutes"
            }
          }
        },
        "loadingDuration": {
          "timeMeasurementUnitCode": {
            "HOUR": {
              "@(2,value)": "loadingDurationInMinutes"
            },
            "*": {
              "@(2,value)": "loadingDurationInMinutes"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "transitDurationInMinutes": "=divide(@(1,transitDurationInMinutes),0.00069444444)",
      "loadingDurationInMinutes": "=divide(@(1,loadingDurationInMinutes),0.01666666666)"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "transitDurationInMinutes": "=toInteger",
      "loadingDurationInMinutes": "=toInteger"
    }
  }
]

最佳答案

另一个选项是分别制作 DAYHOUR 文字对象键,以便在下一步中有条件地选择它们,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "value": "@(1,timeMeasurementUnitCode).&"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "DAY": {
        "val": "=divide(1,@(1,value))",// 1/6 will be used to simulate a multiplication
        "div": "=divide(1440,@(1,val))",
        "transitDurationInMinutes": "=toInteger(@(1,div))"// to eliminate decimal representation which ends with ".0"
      },
      "HOUR": {
        "val": "=divide(1,@(1,value))",
        "div": "=divide(60,@(1,val))",
        "loadingDurationInMinutes": "=toInteger(@(1,div))"
      }
    }
  },
  {// get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*Duration*": "&"
      }
    }
  }
]

网站上的演示 http://jolt-demo.appspot.com/

enter image description here

关于json - Jolt Transformation Spec 中的值相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62076560/

相关文章:

json - 有 JOLT 文档吗? &、@ 等运算符的含义是什么? (NiFi, JoltTransformJSON)

javascript - 在 webpack 的 require 中强制重新缓存 JSON 文件

javascript - 倾斜,调整大小并旋转矩形以完美适合三 Angular 形

json - JOLT 规范根据字符串匹配删除数组元素

python - 在 pandas 的转换中应用几个函数

javax Transformer 保留转义实体

json - JOLT 仅在不为 NULL 时连接

json - 没有嵌套 ng-repeat 的嵌套对象的 Ng-repeat

python - 使用 PRAW 在 Python 中从 Reddit API 解码 JSON

json - 如何创建简单的 Typescript 元数据注释