JOLT 转换数据创建嵌套对

JOLT transform data create nested pairs

我正在尝试转换以下内容

[
  {
    "PLCTime": "1643804542000",
    "LevelID": "53.99.2",
    "Data1Type": "Axis1 Dist",
    "Data1": "1.0",
    "Data2Type": "Axis2 Dist",
    "Data2": "2.0",
    "Data3Type": "Axis3 Dist",
    "Data3": "3.0",
    "Data4Type": "Axis4 Dist",
    "Data4": "4.0",
    "Data5Type": "Axis5 Dist",
    "Data5": "5.5",
    "Data6Type": "Axis6 Dist",
    "Data6": "6.0"
/// etc 20 data points
  }
]

实现所需的输出

{
  "assetId": "53.99.2",
  "dataPoints": [
    {
      "timestamp": "1643804542000",
      "measures": [
        {
          "name": "Axis1 Dist",
          "value": 1
        },
        {
          "name": "Axis2 Dist",
          "value": 2
        }
      ]
    }
  ]
}

LevelID 变成“assetID”,PLCTime 变成“timestamp”

到目前为止我有一个规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "LevelID": "LevelID",
        "*": "dataPoints."
      }
    }
  },
  {
    "operation": "sort",
    "spec": {
      "*": ""
    }
  }
]

它将级别 ID 带到顶层并为我提供数据点数组,但从这里我很难从下面的数据中创建对。

您可以使用两个连续的 shift 转换,其中具有键名称的属性 Data*TypeData* 应该先分开,然后通过生成数组的各自索引再次组合它们(namevalue) 以及使用前缀以生成所需的新数组键名称(measures,dataPoints) 如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Data*Type": {
          "@(0)": "name"
        },
        "Data*": {
          "@(0)": "value"
        },
        "*": "&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "LevelID": "assetId",
      "PLCTime": "dataPoints[].timestamp",
      "name": {
        "*": {
          "@": "dataPoints[0].measures[&].&2",
          "@(3,value[&])": "dataPoints[0].measures[&].value"
        }
      }
    }
  }
]

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