Skip to content
  • There are no suggestions because the search field is empty.

How-To: Convert a Sparkplug Payload to an Array

What Does This Article Cover?

This article provides configuration recommendations and a JavaScript code snippet that can be used to convert a Sparkplug payload to an array. A Sparkplug payload has a hierarchical structure when obtained by an Intelligence Hub Connection Input. In some cases it is beneficial to process the payload as an array. This article provides a solution to achieve this.

What is converting to an array?

In this context converting to an array refers to changing the JSON structure from a hierarchy of objects to an array. Practically this means the following.

Changing a data payload from looking like this.

{
    "node1": {
        "1": {
            "_group": "Simulation_Group",
            "_edgeNode": "node1",
            "_device": "1",
            "value": {
                "Metric": {
                    "value1": 8.99,
                    "value2": 5.97
                }
            }
        },
        "2": {
            "_group": "Simulation_Group",
            "_edgeNode": "node1",
            "_device": "2",
            "value": {
                "Metric": {
                    "value1": 5.33,
                    "value2": 3.02
                }
            }
        }
    }
}

To look like this.

[
    {
        "group": "Simulation_Group",
        "node": "node1",
        "device": "1",
        "metric": "Metric/value1",
        "value": 8.99
    },
    {
        "group": "Simulation_Group",
        "node": "node1",
        "device": "1",
        "metric": "Metric/value2",
        "value": 5.97
    },
    {
        "group": "Simulation_Group",
        "node": "node2",
        "device": "2",
        "metric": "Metric/value1",
        "value": 5.33
    },
    {
        "group": "Simulation_Group",
        "node": "node2",
        "device": "2",
        "metric": "Metric/value2",
        "value": 3.02
    }
]

How to Configure a Solution

In Intelligence Hub versions 3.4 and earlier JavaScript code can be used to alter the format of the payload. The JavaScript code can be applied in Custom Condition or Pipeline Transform Stage.

The following is JavaScript code that could be used in a Pipeline Transform Stage.

// SPB
function transformObjectToArray(obj) {
    let result = [];
    for (let group in obj) {
        for (let eon in obj[group]) {
            // Get the SPB metadata. Requires SPB input to have this option enabled
            let groupName = obj[group][eon]['_group'];
            let edgeNodeName = obj[group][eon]['_edgeNode'];
            let deviceName = obj[group][eon]['_device'];
            let value = obj[group][eon]['value'];
            // Iterate and flatten out the metrics
            let metricResults = flattenObjectWithValues(value, '')
            for (let metricResult of metricResults){
                result.push({
                    group: groupName,
                    node: edgeNodeName,
                    device: deviceName,
                    metric: metricResult.metricPath,
                    value: metricResult.value
                })
            }
        }
    }
    return result;
};
// Helper to flatten metric hierarchy
function flattenObjectWithValues(obj, prefix = '') {
    let result = [];
    for (let key in obj) {
        if (typeof obj[key] === 'object' && obj[key] !== null) {
            result = result.concat(flattenObjectWithValues(obj[key], `${prefix}${key}/`));
        } else {
            result.push({ metricPath: `${prefix}${key}`, value: obj[key] });
        }
    }
    return result;
}
var obj = Object.assign({},event.value);
var resultArray = transformObjectToArray(obj);
stage.setValue(resultArray);

Considerations

The code provided above is formatted for a Pipeline Transform Stage. The code needs to be slightly modified for use in an Intelligence Hub Custom Condition.

Note that this solution assumes that the Include Metadata option is turned on for the Sparkplug Input. This means that the following is included in the payload.

"_group": "Simulation_Group",
"_edgeNode": "node1",
"_device": "1",

Other related material: