JOLT 将展平数组中其他字段的两个值相乘

问题描述 投票:0回答:1

在 JSON 中,我有一个带有公制值的绳索长度,我的任务是生成一个具有英制输出的“双胞胎”。根据“公制”输入中指定的测量单位 (uom),应计算英制值。键中“unit-”后面的值始终是可变的。

这是我输入的 JSON

{
  "unit-1012108-unitNo": "1012108",
  "unit-1012108-description": "IGNITE PROTON WIND",
  "unit-1012108-category": "211",
  "unit-1012108-att-rope_length-identifier": "rope_length",
  "unit-1012108-att-rope_length-type": "attribute",
  "unit-1012108-att-rope_length-attributeDataType": "DECIMAL",
  "unit-1012108-att-rope_length-values-0-value": "5.21",
  "unit-1012108-att-rope_length-values-0-uom": "cm"
}

这是我想要的输出 JSON。稍后可以删除两个辅助字段“-calc-”。 我的Spec的任务应该是计算2.03英寸。

    {
  "unit-1012108-att-rope_length-identifier" : "rope_length",
  "unit-1012108-att-rope_length-type" : "attribute",
  "unit-1012108-att-rope_length-values-0-uom" : "cm",
  "unit-1012108-att-rope_length-values-0-value" : "5.21",
  "unit-1012108-att-rope_length_imp-identifier" : "rope_length_imp",
  "unit-1012108-att-rope_length_imp-type" : "attribute",
  "unit-1012108-att-rope_length_imp-values_0-uom" : "inch",
  "unit-1012108-att-rope_length_imp-values_0-value" : "2.03",
  "unit-1012108-rope_length-calc_imp" : "0.39",
  "unit-1012108-rope_length_calc_metric" : "5.21"
}

我被规范所困。我的问题就在这里。如何将“unit--rope_length-calc_imp”和“unit--rope_length_calc_metric”相乘,然后将值分配给“unit-*-att-rope_length_imp-values_0-value”?这就是我到目前为止所得到的:

    [
  {
    "operation": "shift",
    "spec": {
      "unit-*-att-rope_length-values-0-value": [
        "&",
        "unit-&(0,1)-att-rope_length_imp-values_0-value",
        "unit-&(0,1)-rope_length_calc_metric"
      ],
      "unit-*-att-rope_length-values-0-uom": [
        "&",
        "unit-&(0,1)-att-rope_length_imp-values_0-uom"
      ],
      "unit-*-att-rope_length-identifier": [
        "&",
        "unit-&(0,1)-att-rope_length_imp-identifier"
      ],
      "unit-*-att-rope_length-type": [
        "&",
        "unit-&(0,1)-att-rope_length_imp-type"
      ]
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "unit-*-att-rope_length_imp-identifier": "=concat(rope_length,'_imp')"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "unit-*-att-rope_length_imp-values_0-uom": {
        "m": {
          "#feet": "&2",
          "#3.28": "unit-&(2,1)-rope_length-calc_imp"
        },
        "cm": {
          "#inch": "&2",
          "#0.39": "unit-&(2,1)-rope_length-calc_imp"
        },
        "mm": {
          "#inch": "&2",
          "#0.04": "unit-&(2,1)-rope_length-calc_imp"
        },
        "*": {
          "@(2,&1)": "&2"
        }
      },
      "*": "&"
    }
  },
  {
    "operation": "sort"
  }
]

任何提示将不胜感激。

json jolt
1个回答
0
投票

没有乘法的函数,但可以使用除法函数来模拟,例如

  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "temp_val": "=divide(1,@(1,unit-1012108-att-rope_length-values-0-value))", 
      "unit-1012108-att-rope_length_imp-values_0-value": "=divideAndRound(2,@(1,unit-1012108-rope_length-calc_imp),@(1,temp_val))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "temp_val": ""
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.