1

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 []

1
  • The object is the same yes, I need it to be an array containing the object. It needs to be enclosed in a set of [ ] Commented Feb 24, 2022 at 9:17

1 Answer 1

2

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": {
    "@": "[]"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Both solves the issue as described, I've chosen to use the bottom one. to help clarify the process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.