JOLT 将日期和日期时间解析为 ISO 格式

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

在给定的 JSON 中,我必须将日期调整为 ISO 格式,但我没有找到正确的方法。

这是我输入的json:

{
  "itemNo": "1010333",
  "productNo": "myProduct",
  "description": "myItem-01",
  "attributes": [
    {
      "identifier": "FIRST_DATE",
      "type": "attribute",
      "DataType": "DATE",
      "values": [
        {
          "value": "05.12.2022"
        }
      ]
    },
    {
      "identifier": "mytimestamp",
      "type": "attribute",
      "DataType": "DATEANDTIME",
      "values": [
        {
          "value": "17.07.2023 13:13:10"
        }
      ]
    }
  ]
}

这是所需的输出 当 DataType 为 DATE => 将字符串值解析为 yyyy_MM-ss 当 DataType 为 DATEANDTIME => 将字符串值解析为 yyyy_MM-ss'T'hh:mm:ss

{
  "itemNo": "1010333",
  "productNo": "myProduct",
  "description": "myItem-01",
  "attributes": [
    {
      "identifier": "FIRST_DATE",
      "type": "attribute",
      "DataType": "DATE",
      "values": [
        {
          "value": "2022-12-05"
        }
      ]
    },
    {
      "identifier": "mytimestamp",
      "type": "attribute",
      "DataType": "DATEANDTIME",
      "values": [
        {
          "value": "2023-07-17'T'13:13:10"
        }
      ]
    }
  ]
}

不幸的是,我在这里无法提供丝毫的方法,因为我不知道是从“shift”开始还是从“modify-overwrite-beta”开始,因为事实上,这一切都取决于数据类型。 如果在 JOLT 中转换日期不是一个好主意,我可以尝试使用另一个 nifi 处理器。目前我的目标是,尽可能地使用 JOLT 进行。预先感谢您的意见。

timestamp jolt
1个回答
0
投票

您可以使用 modify-overwrite-beta tarnsformations 来处理该问题,其中您可以首先通过 substringconcat 函数操作字符串,然后检查 hour:min:second 是否存在 部分通过使用

?
运算符,例如在以下情况下:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "attributes": {
        "*": {
          "values": {
            "*": {
              "day_": "=substring(@(1,value),0,2)",
              "mon_": "=substring(@(1,value),3,5)",
              "year_": "=substring(@(1,value),6,10)",
              "hour_": "=substring(@(1,value),11,19)",
              "value": "=concat(@(1,year_),'-',@(1,mon_),'-',@(1,day_))"
            }
          }
        }
      }
    }
  },
  { //adhere 'T' through checking existence of the attribute
    "operation": "modify-overwrite-beta",
    "spec": {
      "attributes": {
        "*": {
          "values": {
            "*": {
              "hour_?": "=concat(''T'',@(1,&))", // occurs only if the "hour_" attribute exist due to the ? operator
              "value": "=concat(@(1,&),@(1,hour_))"
            }
          }
        }
      }
    }
  },
  {//get rid of the attributes end with underscores
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*_": ""
            }
          }
        }
      }
    }
  }
]

网站上的 演示 https://jolt-demo.appspot.com/ 是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.