You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JOLT实现公制转英制长度计算:字段相乘赋值问题

JOLT实现公制绳长转英制字段计算

需求

现有包含公制绳长数据的JSON,需根据指定uom(计量单位)生成对应的英制“孪生”字段,unit-后的数值为可变值。需计算英制长度值(如5.21cm转换为2.03inch),当前卡在如何将unit-*-rope_length-calc_imp与unit-*-rope_length_calc_metric相乘后赋值给unit-*-att-rope_length_imp-values_0-value。

输入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

{"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"}

当前JOLT Spec

[{"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"}]

解决提示

核心问题

当前Spec的问题在于:

  • 初始shift步骤直接给英制value字段赋值了原公制数值,后续无法覆盖
  • 参与计算的字段都是字符串类型,JOLT无法直接对字符串执行乘法
  • 缺少专门的乘法计算步骤

修改后的完整JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "unit-*-att-rope_length-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-*-rope_length_calc_metric": "=toDouble",
      "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.28084": "unit-&(2,1)-rope_length-calc_imp"
        },
        "cm": {
          "#inch": "&2",
          "#0.393701": "unit-&(2,1)-rope_length-calc_imp"
        },
        "mm": {
          "#inch": "&2",
          "#0.0393701": "unit-&(2,1)-rope_length-calc_imp"
        },
        "*": {
          "@(2,&1)": "&2"
        }
      },
      "*": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "unit-*-rope_length-calc_imp": "=toDouble",
      "unit-*-att-rope_length_imp-values_0-value": "=round(=multiply(@(1,unit-&(0,1)-rope_length_calc_metric),@(1,unit-&(0,1)-rope_length-calc_imp)),2)"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "unit-*-att-rope_length_imp-values_0-value": "=toString"
    }
  },
  {
    "operation": "sort"
  }
]

关键步骤说明

  1. 调整初始Shift步骤:移除直接给英制value字段赋值的逻辑,只保留原字段和公制计算字段rope_length_calc_metric
  2. 转换数值类型:用=toDouble把公制数值和转换系数从字符串转成数值类型,确保乘法可执行
  3. 生成转换系数:通过Shift步骤根据uom匹配对应的英制单位和精确转换系数(比如cm转inch用0.393701而非0.39,提升计算精度)
  4. 执行乘法计算:用=multiply计算两个数值的乘积,再用=round保留两位小数,赋值给英制value字段
  5. 转回字符串类型:把计算后的数值转回字符串,匹配期望输出的格式
  6. 排序字段:最后用sort步骤整理字段顺序,和期望输出一致

内容的提问来源于stack exchange,提问作者Katja Bürger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:35:06