Search code examples
azureuser-defined-functionsazure-stream-analytics

How to convert a 'dictionary-like' structure in Azure Stream Analytics to a multi dimensional array with a javascript UDF?


After using the CollectTop aggregation function of Azure Stream Analytics, I get back a json-like structure that seems to be like a Dictionary of Dictionaries. I need to convert this one to a multi-dimensional array which I want to pass to an AzureML UDF.

My question is mostly on how I can interpret this structure in a javascript-UDF, as I'm totally new to Javascript.

This is a sample record (using CollectTop), but the challenge is how my javascript UDF should look like?

[
    {"rank":1,"value":{"engineid":"engine001","tmp":-0.0019,"hum":-0.0002,"eventtime":4}},
    {"rank":2,"value":{"engineid":"engine001","tmp":-0.0026,"hum":-0.0002,"eventtime":2}},
    {"rank":3,"value":{"engineid":"engine001","tmp":0.0003,"hum":-0.0002,"eventtime":1}}
]

From the above data structure, I'd love to get the following array generated. (taking tmp & hum fields)

[[-0.0019, -0.0002], [-0.0026, -0.0002], [0.0003, -0.0002]]

Any help or insights are welcome.

This question is related to two other questions:

Best regards


Solution

  • var input = [
            {"rank":1,"value":{"engineid":"engine001","tmp":-0.0019,"hum":-0.0002,"eventtime":4}},
            {"rank":2,"value":{"engineid":"engine001","tmp":-0.0026,"hum":-0.0002,"eventtime":2}},
            {"rank":3,"value":{"engineid":"engine001","tmp":0.0003,"hum":-0.0002,"eventtime":1}}
        ];
        
        console.log(getOutput(input));
        
        
        function getOutput(input){
            var output = [];
            for(var x in input){
                var array = [];
                array.push(input[x].value.tmp);
                array.push(input[x].value.hum);
                output.push(array);
            }
            return output;
        }

    Is this your needed?