具有数组值的 Jolt 转换规范

时间:2021-03-04 08:18:25

标签: jolt

使用以下输入尝试生成所需的输出,基于值数组的条件不起作用。您能否就此提供一些意见。 根据客户和促销类型需要附加金额值

{
  "Id": "100",
  "name": "Test",
  "discounts": [
    {
      "TypeCode": "customer",
      "Amount": 20
      },
    {
      "TypeCode": "promotion",
      "Amount": 10
    }
   ],
   "items": [
    {
      "LineId": "1001",
      "lineItem": "TestProduct",
      "discountDetails": [
        {
          "LineTypeCode": "customer",
          "discountAmount": 300
        },
        {
          "LineTypeCode": "customer",
           "discountAmount": 330
        }
      ]
    }
  ]
}

所需的输出格式:

{
  "Id": "100",
  "name": "Test",
  customerAmount : 20,
  promotionAmount : 10,
  "items": [
    {
      "LineId": "1001",
      "lineItem": "TestProduct",
      "customerlineamount" : 300,
      "foclineamount" : 330
    }
   ]
}

1 个答案:

答案 0 :(得分:0)

需要使用条件逻辑来提取带有 Amount 键的第三个和第四个元素,例如

"discounts": {
    "*": {
        "TypeCode": {
            "customer": {
                "@(2,Amount)": "customerAmount"
            },
            "promotion": {
                "@(2,Amount)": "promotionAmount"
            }
        }
    }
}

并通过为每个键添加 items 来定义 items.[&1] 数组,这些键应该在结果集中,以便向上一级。因此,请使用以下完整的 Jolt 规范,例如

[{
  "operation": "shift",
  "spec": {
    "Id": "Id",
    "name": "name",
    "discounts": {
      "*": {
        "TypeCode": {
          "customer": {
            "@(2,Amount)": "customerAmount"
          },
          "promotion": {
            "@(2,Amount)": "promotionAmount"
          }
        }
      }
    },
    "items": {
      "*": {
        "LineId": "items.[&1].LineId",
        "lineItem": "items.[&1].lineItem",
        "@(2,items[0].discountDetails[0].discountAmount)": "items.[&1].customerlineamount",
        "@(2,items[0].discountDetails[1].discountAmount)": "items.[&1].foclineamount"
      }
    }
  }
}]
相关问题