首页 > 解决方案 > 数组子段中的 JOLT 转换布尔值

问题描述

我有一个 SAP Idoc,其中包含额外的客户特定字段作为 JSON

{
  "E1EDL20": [
    {
      "VBELN": "1234567890",
      "VSTEL": "ZO01",
      "LGNUM": "123",
      "NTGEW": "100.660",
      "ANZPK": "00002",
      "/XYZ/XYZ1234_E1EDL20": {
        "ACTION": "POSTED",
        "CONSIGNMENT": "12345678",
        "FREIGHT_ORDER_EXTERNAL": "S123456789",
        "IN_YARD": "X",
        "IN_YARD_DATE": "20201123",
        "IN_YARD_TIME": "100923"
      },
      "E1ADRM1": [
        {
          "PARTNER_Q": "LF",
          "PARTNER_ID": "0000100000",
          "/XYZ/XYZ1234_E1ADRM1": {
            "SUPPLIER": "X"
          }
        },
        {
          "PARTNER_Q": "OSP",
          "PARTNER_ID": "ZO01",
          "/XYZ/XYZ1234_E1ADRM1": {
            "SUPPLIER": "X"
          }
        }
      ]
    }
  ]
}

然后我尝试删除根数组,重命名字段并将字符串类型转换为整数、双精度和布尔值,并将日期和时间字段连接到时间戳。这是我的规格。

[
  // Our E1EDL20 array has only one entry -> remove array
  {
    "operation": "cardinality",
    "spec": {
      "E1EDL20": "ONE"
    }
  },
  // Move all contained fields to root level and properly rename them
  {
    "operation": "shift",
    "spec": {
      "E1EDL20": {
        "VBELN": "deliveryInternal",
        "VSTEL": "receivingPoint",
        "LGNUM": "warehouseNumber",
        "NTGEW": "netWeight",
        "ANZPK": "numberOfPackages",
        "/XYZ/XYZ1234_E1EDL20": {
          "ACTION": "action",
          "CONSIGNMENT": "consignment",
          "FREIGHT_ORDER_EXTERNAL": "freightOrderExternal",
          // Rename Content of the field to Boolean String
          "IN_YARD": {
            "X": { "#true": "inYard" },
            "*": { "#false": "inYard" }
          },
          "IN_YARD_DATE": "inYardDateTmp",
          "IN_YARD_TIME": "inYardTimeTmp"
        },
        // Suppliers
        "E1ADRM1": {
          "*": {
            "PARTNER_Q": "supplier[&1].type",
            "PARTNER_ID": "supplier[&1].number",
            "/XYZ/XYZ1234_E1ADRM1": {
              "SUPPLIER": {
                "X": { "#true": "supplier[&2].supplier" },
                "*": { "#false": "supplier[&2].supplier" }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      // Type conversions, String to Double or Integer
      "netWeight": ["=toDouble", null],
      "numberOfPackages": ["=toInteger", null],
      // Type conversions, String to Boolean
      "inYard": ["=toBoolean"],
      "supplier": {
        "*": {
          "belomSupplier": ["=toBoolean"]
        }
      }
    }
  },
  {
    // Create Timestamp for inYardDate
    "operation": "modify-default-beta",
    "spec": {
      "temp_DateYear": "=substring(@(1,inYardDateTmp),0,4)",
      "temp_DateMonth": "=substring(@(1,inYardDateTmp),4,6)",
      "temp_DateDay": "=substring(@(1,inYardDateTmp),6,8)",
      "temp_TimeHours": "=substring(@(1,inYardTimeTmp),0,2)",
      "temp_TimeMinutes": "=substring(@(1,inYardTimeTmp),2,4)",
      "temp_TimeSeconds": "=substring(@(1,inYardTimeTmp),4,6)",
      // Prior to this line, only temporary fields were created from substrings as preparation for the timestamp concatenateion
      "inYardDate": "=concat(@(1,temp_DateYear),'-',@(1,temp_DateMonth),'-',@(1,temp_DateDay),'T',@(1,temp_TimeHours),':',@(1,temp_TimeMinutes),':',@(1,temp_prodTimeSeconds),'Z')"
    }
},
  {
    // Remove temporary substring fields
    "operation": "remove",
    "spec": {
      "temp_DateYear": "",
      "temp_DateMonth": "",
      "temp_DateDay": "",
      "temp_TimeHours": "",
      "temp_TimeMinutes": "",
      "temp_TimeSeconds": "",
      "inYardDateTmp": "",
      "inYardTimeTmp": ""
    }
    }
]

我预计在 JOLT 转换后会有以下输出。

{
  "deliveryInternal" : "1234567890",
  "receivingPoint" : "ZO01",
  "warehouseNumber" : "123",
  "netWeight" : 100.66,
  "numberOfPackages" : 2,
  "action" : "POSTED",
  "consignment" : "12345678",
  "freightOrderExternal" : "S123456789",
  "inYard" : true,
  "inYardDate" : "2020-11-23T10:09:Z",
  "supplier" : [ {
    "type" : "LF",
    "number" : "0000100000",
    "supplier" : true
  }, {
    "type" : "OSP",
    "number" : "ZO01",
    "supplier" : true
  } ]
}

但我得到以下输出。

{
  "deliveryInternal" : "1234567890",
  "receivingPoint" : "ZO01",
  "warehouseNumber" : "123",
  "netWeight" : 100.66,
  "numberOfPackages" : 2,
  "action" : "POSTED",
  "consignment" : "12345678",
  "freightOrderExternal" : "S123456789",
  "inYard" : true,
  "supplier" : [ {
    "type" : "LF",
    "number" : "0000100000"
  }, {
    "type" : "OSP",
    "number" : "ZO01"
  } ],
  "inYardDate" : "2020-11-23T10:09:Z"
}

有谁知道如何解决这个问题?

标签: jsonjolt

解决方案


你只是想增加supplier: true你的供应商吗?我看不出除此之外的任何其他差异。如果是这种情况,您可以将其添加到链规范的末尾:

{
    // Add supplier = true to supplier elements
    "operation": "shift",
    "spec": {
      "supplier": {
        "*": {
          "#true": "supplier[&1].supplier",
          "*": "supplier[&1].&"
        }
      },
      "*": "&"
    }
}

推荐阅读