Home > Net >  JOLT transform object into an array
JOLT transform object into an array

Time:02-25

I am trying to transform the following object into an array, the object is formatted already as required just needs to be output as an array containing that object.

[
  {
    "PLCTime": 1643804542000,
    "LevelID": "53.99.2",
    "Data1Type": "Axis1 Dist",
    "Data1": 1,
    "Data2Type": "Axis2 Dist",
    "Data2": 2,
    "Data3Type": "Axis3 Dist",
    "Data3": 3,
    "Data4Type": "Axis4 Dist",
    "Data4": 4,
    "Data5Type": "Axis5 Dist",
    "Data5": 5.5,
    "Data6Type": "Axis6 Dist",
    "Data6": 6
  }
]

I would like the output to be an array containing the one object. Currently my spec is:

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

Which gets me the following, but you can see the result is not an array.

{
  "assetId": "53.99.2",
  "dataPoints": [
    {
      "timestamp": 1643804542000,
      "measures": [
        {
          "name": "Axis1 Dist",
          "value": 1
        },
        {
          "name": "Axis2 Dist",
          "value": 2
        },
        {
          "name": "Axis3 Dist",
          "value": 3
        },
        {
          "name": "Axis4 Dist",
          "value": 4
        },
        {
          "name": "Axis5 Dist",
          "value": 5.5
        },
        {
          "name": "Axis6 Dist",
          "value": 6
        }
      ]
    }
  ]
}

The output I am trying for is :

[
  {
    "assetId": "53.99.2",
    "dataPoints": [
      {
        "timestamp": 1643804542000,
        "measures": [
          {
            "name": "Axis1 Dist",
            "value": 1
          },
          {
            "name": "Axis2 Dist",
            "value": 2
          },
          {
            "name": "Axis3 Dist",
            "value": 3
          },
          {
            "name": "Axis4 Dist",
            "value": 4
          },
          {
            "name": "Axis5 Dist",
            "value": 5.5
          },
          {
            "name": "Axis6 Dist",
            "value": 6
          }
        ]
      }
    ]
  }
]

e.g which needs to be enclosed in a set of []

CodePudding user response:

You can

  • prefix the identifiers on the right-hand side with [0]. for the second tranformation spec in order to nest the whole content within the square brackets such as
{
  "operation": "shift",
  "spec": {
    "LevelID": "[0].assetId",
    "PLCTime": "[0].dataPoints[].timestamp",
    "name": {
      "*": {
        "@": "[0].dataPoints[0].measures[&].&2",
        "@(3,value[&])": "[0].dataPoints[0].measures[&].value"
      }
    }
  }
}

or

  • add the following spec to the current ones, alternatively
{
  "operation": "shift",
  "spec": {
    "@": "[]"
  }
}
  • Related