希望我正确地描述了这一点:是否可以仅从属性值创建列和行?在我当前的代码中,列是属性名称,行是值。Current OutputPreferred Output
JSON消息:{deviceName:“ FillerDevice”,时间戳:“ 2019-07-17T16:42:10Z”,标签:[{do4:1},{do5:0}]}
以下方法为数组中每个定义的属性键创建列。
您可以使用用户定义的函数来实现,前提是您事先知道要查找的键:
ASA查询
select
Input.deviceName,
Input.timestamp,
UDF.ExtractData(Input.tags, 'do4') as do4,
UDF.ExtractData(Input.tags, 'do5') as do5
into Output
from Input
UDF
function ExtractData(array, key) {
'use strict';
var value = null;
if (array != null
&& key != null
&& Array.isArray(array)
&& isString(key)) {
// find all objects indexes of objects in the array with the key
var indexes = [], i;
for (i = 0; i < array.length; i++) {
if (array[i].hasOwnProperty(key)) {
indexes.push(i);
}
}
// in case it occurs multiple times take the first one
// you could also implement other conditions here
var indexToUse = indexes[0];
// set return value
if (indexes.length > 0) {
value = array[indexToUse][key];
}
}
return value;
}
function isString(value) {
return typeof value === 'string' || value instanceof String;
}