JOLT变换-模式匹配

时间:2019-11-16 07:27:13

标签: jolt

我正在尝试通过匹配模式来转换输入字段,例如“ | | *”,其中单词之间用“ |”分隔。但是,我无法使其正常运行。如果我替换“ |”按“:”,则说明有效。 “ |”是否有特殊含义,如何将其用作正则表达式的一部分?

Input:
{
  "items": [
    {
      "itemId": "1234500AA",
      "additionalData": "12345|20001|3000"
    }
  ]
}

Spec:
Spec:
[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "itemId": "ID",
          "additionalData": {
            "*|*|*": {
              "$(0,1)": ["cartMenuItems[&3].baseProductId", "cartMenuItems[&3].mdmId"],
              "$(0,2)": "cartMenuItems[&3].code",
              "$(0,3)": "cartMenuItems[&3].sku"
            }
          }
        }
      }
    }
  }
]

Ouput Expected:
{
  "ID" : "1234500AA",
  "cartMenuItems" : [ {
    "baseProductId" : "12345",
    "mdmId" : "12345",
    "code" : "20001",
    "sku" : "3000"
  } ]
}

但是,出现如下错误: JOLT Chainr在构造索引为0的Transform className:com.bazaarvoice.jolt.Shiftr时遇到异常。

1 个答案:

答案 0 :(得分:0)

震动似乎不喜欢| (因为您需要对其进行转义),可以替换|:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "items": {
        "*": {
          "additionalDataTemp": "=split('\\|',@(1,additionalData))",
          "additionalData": "=concat(@(1,additionalDataTemp[0]),'_',@(1,additionalDataTemp[1]),'_',@(1,additionalDataTemp[2]))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "itemId": "ID",
          "additionalData": {
            "*_*_*": {
              "$(0,1)": ["cartMenuItems[&3].baseProductId", "cartMenuItems[&3].mdmId"],
              "$(0,2)": "cartMenuItems[&3].code",
              "$(0,3)": "cartMenuItems[&3].sku"
            }
          }
        }
      }
    }
  }
]

或者另一种方式是:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "items": {
        "*": {
          "additionalData": "=split('\\|', @(1,additionalData))",
          "baseProductId": "@(1,additionalData[0])",
          "mdmId": "@(1,additionalData[0])",
          "code": "@(1,additionalData[1])",
          "sku": "@(1,additionalData[2])"
        }
      }
    }
    },
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "itemId": "ID",
          "*": "cartMenuItems.&"
        }
      }
    }
    },
  {
    "operation": "remove",
    "spec": {
      "cartMenuItems": {
        "additionalData": ""
      }
    }
    }

]