Wrapping JSON into output Stream Analytics query

我的未来我决定 提交于 2019-12-08 09:37:13

问题


I am using Stream Analytics query to filter my inputted Complex Json object.

Input:

    {
  "id" : "001",
  "firstArray":[
    {
      "tid" : 9,
       "secondArray":[
         {
            "key1" : "value1",
            "key2" : "value2"
         },
         {...}
       ]
     },
     {
      "tid" : 8,
       "secondArray":[
         {
            "key1" : "value1",
            "key2" : "value2"
         },
         {...}
       ]
     }
  ]
}

This is my query:

   WITH T1 AS 
    (
    SELECT 
        FirstArray.ArrayValue.Tid as Tid,
        FirstArray.ArrayValue.secondArray as SecondArray
    FROM
        inputfromeventhub MySource
    OUTER APPLY GetElements(MySource.firstArray) AS FirstArray
    )
SELECT
   T1.Tid as Tid,
   SecondArray.ArrayValue.key1 as key1,
   SecondArray.ArrayValue.key2 as key2
INTO exitstream
OUTER APPLY GetElements(T1.SecondArray) as SecondArray

I get something like this:

[
 {
  "tid":9,
  "key1": "value1",
  "key2": "value2"
 },
 {
  "tid":8,
  "key1": "value1",
  "key2": "value2"
 }
]

I want to wrap this JSON Array into a JSON Object with a unique 'id' to get something like this:

{
 "id":"001",
 "array":[
  {
   "tid":9,
   "key1": "value1",
   "key2": "value2"
  },
  {
   "tid":8,
   "key1": "value1",
   "key2": "value2"
  }
 ]
} 

I cant find a way to do that. I tried creating a third select that calls a user defined function:

function main(obj) {
  var out_obj = {};
  out_obj.id = "001";
  out_obj.array = obj;

  return JSON.stringify(out_obj);
}

but this is applied to each object in the array.. so I get this:

[
  { "myFunction": "{\"id\":\"001\",\"array{\"tid\":9,\"key1\":\"value1\",\"key2\":\"value2\"}"
  },
  { "myFunction": "{\"id\":\"001\",\"array{\"tid\":8,\"key1\":\"value1\",\"key2\":\"value2\"}"
  }
]

Is there a way to wrap all the nested objects in that array with a query?


回答1:


According to your requirement, I tested this issue, you could refer to the following snippet code:

javascript UDF

function main(obj) {
  var out_obj = [];
  for(var i=0;i<obj.length;i++){
    var o=obj[i];
    for(var j=0;j<o.secondArray.length;j++){
      o1=o.secondArray[j];
      out_obj.push({tid:o.tid,key1:o1.key1,key2:o1.key2});
    }
  }
  return out_obj;
}

QUERY

SELECT 
    MySource.id,
    udf.aggreate(MySource.firstArray) as array
FROM
    [inputfromeventhub] MySource

Input

{
  "id" : "001",
  "firstArray":[
    {
      "tid" : 9,
       "secondArray":[
         {
            "key1" : "value01",
            "key2" : "value02"
         },
         {
            "key1" : "value03",
            "key2" : "value04"
         }
       ]
     },
     {
      "tid" : 8,
       "secondArray":[
         {
            "key1" : "value11",
            "key2" : "value12"
         },
         {
            "key1" : "value13",
            "key2" : "value14"
         }
       ]
     }
  ]
}

Output



来源:https://stackoverflow.com/questions/43557900/wrapping-json-into-output-stream-analytics-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!