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?
According to your requirement, I tested this issue, you could refer to the following snippet code:
javascript UDF
QUERY
Input
Output