Trying to stand up a relatively simple ASA query to pull data being ingested as an Array. The piece I cant seem to figure out is how to have the returned table auto populate columns for each Key. Meaning we need each row to have a column for Temperature Value, Humidity Value, Illumination Value, and Contact Value if it exists. Some other arrays will only have Key: Temperature and Key: Humidity.
Thanks!
"telemetry": {
"data": [
{
"key": "temperature",
"value": 19.1,
"unit": "°C"
},
{
"key": "humidity",
"value": 38.5,
"unit": "%"
},
{
"key": "illumination",
"value": 0,
"unit": "lx"
},
{
"key": "accelerationStatus",
"value": "heartbeat",
"meaning": "Heartbeat"
},
{
"key": "accelerationX",
"value": -0.02,
"unit": "g"
},
{
"key": "accelerationY",
"value": 0,
"unit": "g"
},
{
"key": "accelerationZ",
"value": 1.01,
"unit": "g"
},
{
"key": "contact",
"value": "open",
"meaning": "Window opened"
}
],
You could use UDF in ASA.
UDF function code:
function main(arg) {
var array = arg.telemetry.data;
var map = {};
for(var i=0;i<array.length;i++){
var key=array[i].key;
if(key == "temperature" || key == "humidity" || key == "illumination"){
map[key] = array[i].value + "" + array[i].unit;
}
if(key == "contact"){
map[key] = array[i].value;
}
}
return map;
}
SQL:
WITH
c AS
(
SELECT
udf.processArray(t) AS result
FROM input AS t
)
SELECT c.result
FROM c
Result: