Home > Blockchain >  JOLT transform data create nested pairs
JOLT transform data create nested pairs

Time:02-20

I am trying to convert the following

[
  {
    "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
  }
]

to achive this as desired output

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

Where the LevelID becomes "assetID" and PLCTime becomes "timestamp"

So far I have for a spec:

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

which brings the level ID to the top level and gives me and array of data points but from here I am struggling to create pairs from the data below.

CodePudding user response:

You can use two successive shift transformations in which the attributes with the key names Data*Type versus Data* should be seperated first, then combine them back again by their respective indexes of the generated arrays(name and value) along with using prefixes in order to generate the desired new array key names(measures,dataPoints) such as

[
  {
    "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[0])": "dataPoints[0].measures[&].value"
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related